How to NotifyOfPropertyChange two different views if one of them changes

Sep 20, 2010 at 4:12 PM

Hi, I have an architectural problem concerning updating views.

Let’s say I have two views in my Shellview: LeftView and RightView. Both of them display a Name found in the Model. LeftViewModel fetches the Name from the model and display it in a TextBox in LeftView and RightViewModel display Name in RightView. I type a new Name in the TextBox in LeftView and hence changes Name in the Model but RightView does not change :( A NotifyOfPropertyChange in LeftViewModel only changes objects in LeftView. A NotifyOfPropertyChange in Model changes nothing since no one has Model as Context.

                    ___________
                    |_Model__|
                    /             \
  _____________/______   _____\________________
  |LeftViewModel_|   |_RightViewModel_|
   ______|_____               ______|________
   |_LeftView_|              |_RightView_|

I can think of 2 solutions but I haven’t seen anyone else use these so I suspect they are wrong:

1. Make RightViewModel listen for changes in the Model
2. Use EventAggregator to signal to RightViewModel that changes have been made.

Anyone know the answer?

Thanx

/Mike

Sep 20, 2010 at 7:55 PM
Edited Sep 20, 2010 at 7:55 PM

I personally prefer #1.  If you implement INotifyPropertyChanged in the model, each view-model can listen for the OnPropertyChanged event.

And if you do that, consider binding the views directly to the model for Name.    So, both view-models could have a textbox named Subject_Name (if each view-model has a Subject property that exposes the model).  Usually my view-models don't need to listen for events if I do it that way.

Hope this helps!

Brandon

Sep 20, 2010 at 8:31 PM

I though your patience with the ASCII-art deserved another opinion (for what it's worth) :-)

I agree with @bondsbw: for most scenarios is simpler to just expose the (domain) model as Subject and bind directly to it. Echoing the model property into a VM property seems superfluous to me.
I would choose an EventAggregator approach only when dealing with models not supporting change notification on their own.

Sep 21, 2010 at 10:46 AM

Thanx guys. Glad you like my ASCII-art :)

I go with #1 then, good (Can’t bind directly, need to convert it some).

It would be great to hear you guys opinion on a similar problem:

Say I have a ParentView and inside of this one a ChildView. Both display Name from the Model in a TextBox. I change Name in ChildView and want it to get updated in ParentView.

I could solve this in the same way as #1 above but the Child-Parent relationship opens for 2 new options.

#1 Same as #1 above

#2 The ChildViewModel have an instance of ParentViewModel and passes ParentViewModels Name like this:

                     public string Name { get { return MyParentViewModel.Name; } set { MyParentViewModel.Name = value } }

#3 ParentViewModel have an instance of ChildViewModel and listen for changes in ChildViewModel.

 

#1 is probably the best but I want to hear what you think. I’m new to MVVM and I want to do things right.

Sep 21, 2010 at 1:57 PM

I cannot guarentee this is the Right Way™ to go, but I would choose the option #1: when I have a common value that could be viewed and changed in different screens, I usually share a single instance of the owner of the information itself so that the synchronization is done with no additional work (if the owner supports change notification).

Sep 22, 2010 at 9:16 AM

Ok, great, now I know how to solve these type of problems :)