Best practice for ViewModel listening for others' RaisePropertyChanged?

Mar 24, 2010 at 8:03 PM

Thanks for providing this framework.  I had previously been using MVVM Foundation, but decided to make the jump to this one because it offers a few additional features which I think I'll need. 

In my ViewModels, I had been using PropertyObserver to listen for PropertyChanged events on other VMs.  Since MVVM Light does not seem to have a counterpart to this, what is the recommended way to achieve this?

Here is an example scenario:  Suppose I have a hierarchy or relatives; Parent, Child, Grandchild.  I would like the Parent ViewModel to expose properties called "ChildCount" and "GrandchildCount" and fire PropertyChanged events for the UI to pick up. 

ChildCount is easy because Parent fires a PropertyChanged event whenever a Child is added/removed.  It gets complicated because I want Parent to fire a "GrandchildCount" PropertyChanged event whenever one of its Children adds/removes a Grandchild.  Right now the only way I can see to do this is in Parent, manually implement an IEventListener for each Child's ChildCount PropertyChanged event.  This is conceptually what Josh Smith's PropertyObserver does.

I made a sample program demonstrating this:

It would make my day if someone can explain that I'm approaching this the wrong way, and there is actually a much easier way to do it. 



Mar 24, 2010 at 8:10 PM


I am very aware of Josh's work, as he is a colleague of mine at IdentityMine, a good friend, and someone I have been talking to about MVVM for multiple years now :) As of now, I would recommend using his PropertyObserver. This is a component that I am planning on integrating (aka stealing) in a future version of the toolkit. Josh's license allows you to copy the code into your own application, if you do not want to include an additional DLL.

Another approach would be to use the Messenger to send messages when an event occurs. The ViewModelBase class has two overloads of the RaisePropertyChanged method. The second one (the one with oldValue and newValue) can broadcast a PropertyChangedMessage<T> to any listener that registered for a message of this type. However the risk of losing the overview on what you're doing is a bit bigger with the Messenger, because it is so much decoupled.

Hope this helps,


Mar 24, 2010 at 8:33 PM


Thanks for the amazingly fast reply. 

It had crossed my mind to continue using the PropertyObserver as you suggest, but I was thought it meant I was missing a piece of the puzzle while trying to grasp your MVVM Light framework.  Thanks for the reassurance. 

I considered using the overload which broadcasts, but I agree that it could get messy when I have multiple instances of Parent.  I also considered using Notification Messages, since it seems like the right tool for the job.  It would require two lines of code though; one to inform the UI, and another to inform the VMs:

RaisePropertyChanged("ChildCount");  //UI picks this up
Messenger.Default.Send(new NotificationMessage(this, "ChildCount"));  //Parent VM listens for this message, then checks if sender is type Child

I'll stick with Josh's PropertyObserver for now as you recommend; I look forward to future MVVM Light releases.