简体   繁体   English

视图模型不会更新到 DataContext

[英]View model doesn't get updated to DataContext

I have a working program where the view model feed the data to the view.我有一个工作程序,其中视图模型将数据提供给视图。 This is done in the constructor and also in a Refresh() method, as shown below.这是在构造函数和Refresh()方法中完成的,如下所示。 I usually put a private property referring to the view model because I want to omit casting when communicating with it.我通常会放置一个引用视图模型的私有属性,因为我想在与它通信时省略强制转换。

public class TheView
{
  private ViewModel TheViewModel { get; set; }

  public TheView()
  {
    TheViewModel = new ViewModel();
    DataContext = TheViewModel;
  }

  public Refresh()
  {
    TheViewModel = new ViewModel();
    DataContext = TheViewModel;
  }
}

Then, I got gready and started chasing lines.然后,我变得贪婪并开始追逐线条。 Since the constructor connects DataContext to the property TheViewModel , I figured I could just assign to the latter and the former would get it's stuff updated by itself.由于构造函数将DataContext连接到属性TheViewModel ,我想我可以只分配给后者,而前者会自行更新它的内容。 To my disappointment, I discovered that it wasn't so.令我失望的是,我发现事实并非如此。 The following get me the correct list of objects but the DataContext stays unaffected.以下内容为我提供了正确的对象列表,DataContext不受影响。

public class TheView
{
  private ViewModel TheViewModel { get; set; }

  public TheView()
  {
    TheViewModel = new ViewModel();
    DataContext = TheViewModel;
  }

  public Refresh() { TheViewModel = new ViewModel(); }
}

The question is why it is so.问题是为什么会这样。 Or rather, if it's supposed to be so.或者更确切地说,如果它应该是这样的话。 I'm thinking, in case it isn't supposed to behave like this, perhaps I have issues elsewhere in the code that poofs the flow...我在想,如果它不应该表现得像这样,也许我在代码中的其他地方有问题会导致流程不畅......

I think you do need understand that DataContext is a dependency property.我认为您确实需要了解DataContext是一个依赖属性。

Its definition looks like this:它的定义如下所示:

[DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]
[Localizability(LocalizationCategory.NeverLocalize)]
public object DataContext
{
  get
  {
    return this.GetValue(FrameworkElement.DataContextProperty);
  }
  set
  {
    this.SetValue(FrameworkElement.DataContextProperty, value);
  }
}

... ...

    public static readonly DependencyProperty DataContextProperty = 
DependencyProperty.Register("DataContext", typeof (object...

Dependency properties are more than normal .NET properties because they support依赖属性不仅仅是普通的 .NET 属性,因为它们支持

  • change notification变更通知
  • property value inheritance属性值继承
  • providers which determine the value决定价值的提供者

In your case it is the "change notification" topic which is important.在您的情况下,重要的是“更改通知”主题。

When you (re-) assign a value to a property which is a dependency property (like DataContext ) then WPF will pick up this change automatically.当您(重新)为作为依赖属性的属性(如DataContext )分配值时,WPF 将自动获取此更改。 In this case the change notifcation is done inside the setter of the dependency property so to speak.在这种情况下,可以这么说,更改通知是在依赖属性的 setter 中完成的。

If you assign to a "normal" property or even a private field this is not the case (the setter of the DataContext property is not invoked) even if it points to a DataContext.如果您分配给“普通”属性甚至私有字段,则情况并非如此(不调用DataContext属性的 setter),即使它指向 DataContext。

You are basically re-assigning a private property, not the DataContext.您基本上是在重新分配私有属性,而不是 DataContext。

  1. you create instance A of TheViewModel您创建TheViewModel实例 A
  2. you make the DataContext point to instance A你让 DataContext 指向实例 A
  3. you create instance B of TheViewModel您创建TheViewModel实例 B
  4. you make the TheViewModel private property point to instance B您使TheViewModel私有属性指向实例 B
  5. DataContext is still pointing to instance A DataContext 仍然指向实例 A

--> there is no reason why the DataContext should be refreshed. --> 没有理由应该刷新 DataContext。 you didn't touch it.你没有碰它。 it's still pointing to the initial instance A, which is still in memory.它仍然指向仍在内存中的初始实例 A。

EDIT :编辑

That answer was assuming the binding was using DataContext as a source, and not TheViewModel .这个答案是假设绑定使用DataContext作为源,而不是TheViewModel DataContext is a DependencyProperty, whereas TheViewModel isn't. DataContext是一个 DependencyProperty,而TheViewModel不是。 Replacing the whole view model with TheViewModel as a binding source needs to notify the view either with a dependency property or NotifyPropertyChangedTheViewModel作为绑定源替换整个视图模型需要使用依赖属性或NotifyPropertyChanged通知视图

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

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