0

Suppose I have two ViewModels and both are using same Model class. If one ViewModel changes some property value in Model class, I want to Notify the same to second ViewModel, so that I can show the changes in Views without refreshing it again.

Model<-------->ViewModel 1
  ^
  |------------>ViewModel 2

Please help me, how to implement this (Model<--->ViewModel(s) Two-Way binding) using MVVM pattern? And if there is some other way which is more feasible, will be much thankful.

Sandy
  • 6,285
  • 15
  • 65
  • 93
  • `INotifyPropertyChanged` is one way. Having dedicated events for each type of property that changed is another. Or a single "something changed; you should refresh" event is yet another. – Peter Torr - MSFT Jul 14 '16 at 05:59

1 Answers1

1

I think you misunderstood with Model<--->ViewModel(s) Two-Way binding, actually the binding source should at least be an instance of the Model, the DataContext should be the ViewModel which contains this instance of the Model, we can't directly bind model to a binding target. So your design pattern is not quite right.

I think what you need is like when data is changed in ViewModel1, other ViewModels can get notified and response to it, and you may have done this work by manually refresh it and you want to find another way.

Here is an easy way to do this in MVVM pattern, you can use the Messenger of MVVM Light, you can refer to this question on SO: Use MVVM Light's Messenger to Pass Values Between View Model.

Community
  • 1
  • 1
Grace Feng
  • 16,564
  • 2
  • 22
  • 45