Ankit Saini
Ankit Saini

Reputation: 63

How to Notify all ViewModels about the changes done in properties of Model class by one ViewModel in UWP using MVVM pattern

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.

Upvotes: 0

Views: 244

Answers (1)

Grace Feng
Grace Feng

Reputation: 16652

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.

Upvotes: 1

Related Questions