I am trying to synchronize data between seperate Views in MVVM.
Here is the scenario:
There’re two ViewModels and two Views respectively:
ViewModel1: contains a list of “Person” object, named “People”, which is defined in Entity Framework and is retrieved via WCF Service.
View1: contains an ItemsControl wich is bound to “People” of ViewModel. Item’s color may vary depending on its value, for example: person who is over 60 years old may appear red color, while person under 18 years old may appear green. This is achieved by a Value Converter.
(there is a button named “Show Detail Info” in View1, when it’s clicked, a ChildWindow dialog is poped up and detail information of currently selected “Person” is displayed )
ViewModel2: contains a “SelectedPerson” object, and implements a “Save” method.
View2: contains several input fields bound to corresponding fields of “SelectedPerson” object, such as TextBox for “Person.FirstName”, DateTimePicker for “Person.Birthday”, RadioButton for “Person.Gender”, etc.
Problem:
when i changed some fields (i.e. “Name” field) and clicked “Save” button, i can even see that changes have been committed onto Database. However, the corresponding item in View1 failed to update its color.
Is there better way to fix this problem?
You can adress such problems usually in three ways depending on your setup:
1 & 2) If the 2 VMs are instantiated by the same parent object these can be connected via
INotifyPropertyChangedorEventsfor the updated Properties3) If they are disconnected you can use an EventAggregator to message between the VMs. CaliburnMicro has a pretty good implementation but you can build one yourself with just two classes. For more information see: Caliburn.Micro Soup to Nuts Part 8–The EventAggregator