简体   繁体   English

如何在 MVVM 中的不同视图之间同步(模型)数据

[英]How to synchronize (Model)data between difference Views in MVVM

I am trying to synchronize data between seperate Views in MVVM.我正在尝试在 MVVM 中的不同视图之间同步数据。

Here is the scenario:这是场景:

There're two ViewModels and two Views respectively:分别有两个 ViewModel 和两个 View:

ViewModel1: contains a list of "Person" object, named "People", which is defined in Entity Framework and is retrieved via WCF Service. ViewModel1:包含一个名为“People”的“Person”对象列表,它在实体框架中定义并通过 WCF 服务检索。

View1: contains an ItemsControl wich is bound to "People" of ViewModel. View1:包含一个绑定到 ViewModel 的“人物”的 ItemsControl。 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.物品的颜色可能会根据其价值而有所不同,例如:60 岁以上的人可能呈红色,而 18 岁以下的人可能呈绿色。 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 ) (View1中有一个名为“Show Detail Info”的按钮,点击后会弹出ChildWindow对话框,显示当前选中的“Person”的详细信息)

ViewModel2: contains a "SelectedPerson" object, and implements a "Save" method. ViewModel2:包含一个“SelectedPerson”对象,并实现一个“Save”方法。

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. View2:包含多个输入字段绑定到“SelectedPerson”对象的相应字段,例如“Person.FirstName”的TextBox,“Person.Birthday”的DateTimePicker,“Person.Gender”的RadioButton等。

Problem:问题:

when i changed some fields (ie "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.但是,View1 中的相应项未能更新其颜色。

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 INotifyPropertyChanged or Events for the updated Properties 1 & 2)如果 2 个 VM 由同一个父对象实例化,则它们可以通过INotifyPropertyChanged或更新属性的Events连接

3) If they are disconnected you can use an EventAggregator to message between the VMs. 3)如果它们断开连接,您可以使用EventAggregator在 VM 之间发送消息。 CaliburnMicro has a pretty good implementation but you can build one yourself with just two classes. CaliburnMicro 有一个很好的实现,但你可以只用两个类自己构建一个。 For more information see: Caliburn.Micro Soup to Nuts Part 8–The EventAggregator有关更多信息,请参阅: Caliburn.Micro Soup to Nuts 第 8 部分——EventAggregator

It's hard to answer without knowing exactly how your SelectedPerson and your People list are related.如果不确切知道您的SelectedPerson和您的People列表是如何相关的,就很难回答。 So I assume that SelectedPerson is one item out of your People list.所以我假设SelectedPerson是您的People列表中的一项。 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.此信息通常使用实现 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.每次属性更改时,在您的情况下PersonViewModel的属性AgePersonViewModel引发 PropertyChanged 事件,因此所有绑定项目都知道值更改,因此将重新查询它们的值。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM