mardi 3 mars 2015

Hierarchical ViewModels and tracking IsDirty/PropertyChanged events


I have a set of ViewModels depicted like in the below picture.


Structure of ViewModels


And, I have a couple of commands AcceptCommand, CancelCommand in the Top Level View Model that are bound to Apply, Cancel buttons respectively in a View.


I would like to enable Apply, and Cancel buttons only when any of the Child Level (Level 1 and Level 2) View Models are dirty.


Currently, I am thinking of two possible solutions to achieve this behavior:


1) Have ViewModels at each Child level raise a PropertyChanged/IsDirty event and handle that event at it's parent level and mark the parent as dirty and then continue this process until it reaches the Top Level View Model.


2) I could have all the Child Level view models to post a message to indicate their dirty/clean state on some Mediator/Messenger and have the Top Level ViewModel listen for those message and mark it dirty/clean based on a count in the Top Level View Model depending on how many dirty/clean messages it has received.


Though, the second option looks better than the first one. I do not feel any of those two approaches are optimal.


Are there any better alternatives/patterns to approach and solve this problem?





Aucun commentaire:

Enregistrer un commentaire