MVVM:修改后的模型,如何正确地更新视图模型和视图? [英] MVVM: Modified model, how to correctly update ViewModel and View?

查看:500
本文介绍了MVVM:修改后的模型,如何正确地更新视图模型和视图?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

说我有一个类, PersonViewModel PersonView

从更新属性 PersonView 模型是很简单的。 PersonViewModel 包含对象,具有公共属性的 PersonView 结合以更新Person模型。

Updating properties from PersonView to the Person model is simple enough. PersonViewModel contains a Person object and has public properties the PersonView binds to in order to update the Person model.

不过。

想象模型可以通过服务得到更新。现在的属性改变需要传达到 PersonViewModel 然后到 PersonView

Imagine the Person model can get updated by Service. Now the property change needs to be communicated to the PersonViewModel and then to the PersonView.

这是我会怎么解决这个问题:

This is how I would fix it:

有关人员每个属性我的模型将提高PropertyChanged事件。 PersonViewModel 订阅的PropertyChanged事件。 PersonViewModel 然后将再多问的PropertyChanged以更新 PersonView

For each property on the Person model I would raise the PropertyChanged event. PersonViewModel subscribes to the PropertyChanged event of Person. PersonViewModel would then raise another PropertyChanged in order to update the PersonView.

这对我来说似乎是最明显的方式,但我有种想在别人希望我展示一个更好的出路有抛出这个问题。难道真是这个简单的还是有更好的方式来纪念模型修改和更新的视图模型的相应属性?

This to me seems the most obvious way but I kind of want to throw this question out there in the hope of someone showing me a nicer way. Is it really this simple or are there better ways to mark the model as modified and update the respective properties on the ViewModel?

PersonView 的DataContext的是 PersonViewModel 会从JSON填充,并在其一生中得到更新很多次了。

The PersonView's DataContext is PersonViewModel. Person gets populated from JSON and gets updated many times during its lifetime.

随时提出了architectual改变我特殊情况。

Feel free to suggest architectual changes for my particular case.

标志着我aqwert我的问题的答案,因为它与另一种为我提供到我已经提出了解决方案。

I marked aqwert as the answer of my question since it provided me with an alternative to the solution I already proposed.

推荐答案

如果视图直接这时只要绑定到模型作为服务使用同一个实例的模特属性的任何更改将传播到该视图。

If the view is binding to the Model directly then as long as the service is using the same instance any changes to the model properties will be propogated to the view.

然而,如果你重新在服务的新模式,那么你将会给视图模型的新模式。我希望看到的模型作为视图模型的属性,所以当你设置该属性的所有绑定应警惕的变化。

However if you are recreating a new model in the service then you will give the viewmodel the new model. I expect to see the model as a property on the view model so when you set that property all binding should be alerted to the change.

//in the ViewModel
public Person Model
{
   get { return _person; }
   set { _person = value; 
         RaisePropertyChanged("Model");  //<- this should tell the view to update
        }
}

编辑:

既然你说出具体有视图模型逻辑,那么你可以在<$ C定制这些属性$ C>视图模型

Since you state there are specific ViewModel logic then you can tailor those properties in the ViewModel

 private void Model_PropertyChanged(object sender, PropertyChangedEventArgs e)
 {
      if(e.PropertyName == "Prop1") RaisePropertyChanged("SpecicalProperty");
      ...
 }

  public string SpecicalProperty
  {
     get
     {
         reutrn Model.Prop1 + " some additional logic for the view"; 
     }
   }

在XAML

  <TextBlock Text="{Binding Model.PropertyDirect}" />  
  <TextBlock Text="{Binding SpecicalProperty}" />

这仅仅两个模式视图模型 propertys被绑定到视图,而不复制数据。

This way only both the Model and ViewModel propertys are bound to the view without duplicating the data.

您可以得到票友创建一个助手从模型链接属性更改到视图模型或使用映射字典

You can get fancier creating a helper to link the property changes from the model to the view model or use a mapping dictionary

 _mapping.Add("Prop1", new string[] { "SpecicalProperty", "SpecicalProperty2" });



,然后找到属性通过获取的属性列表

and then find the properties to update by getting the list of properties

 private void Model_PropertyChanged(object sender, PropertyChangedEventArgs e)
 {

      string[] props;
      if(_mapping.TryGetValue(e.PropertyName, out props))
      {
          foreach(var prop in props)
              RaisePropertyChanged(prop);
      } 
 }

这篇关于MVVM:修改后的模型,如何正确地更新视图模型和视图?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆