2
votes

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?

2
Check out my answer here - it solves a similar problem stackoverflow.com/questions/7995517/…Steve Greatrex
Is the list of "Person" in VM1 an ObservableCollection? If not change it to an ObservableCollection. My assumption is that changes to your list are not propagated to the UI.SvenG
@SvenG ObservableCollection will not throw any event if an item changes it's internal state without propagating this change.PVitt
How is the list of Person objects created? are you using DataSets/DataTables?jpsstavares
@Steve Greatrex Thank you for your suggestion. Your reply help me to and lead me to get solution.Pongsathon.keng

2 Answers

2
votes

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 INotifyPropertyChanged or Events for the updated Properties

3) 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

0
votes

It's hard to answer without knowing exactly how your SelectedPerson and your People list are related. So I assume that SelectedPerson is one item out of your People list. If this is the case, your problem is that your view control that displays the colour depending on the persons age doesn't get informed about the value change.

This information is usually done with a view model that implements INotifyPropertyChanged. Each time a property changes, in your case the property Age of your PersonViewModel, the PropertyChanged event is raised and thus all bound items know about the value change and therefore will requery their values.