刷新WPF中的视图的DataContext?

时间:2022-08-24 23:07:18

I am working with a WPF application and using my own architecture that strongly resembles a M-V-VM /MVC. I have controllers for each of the views, and I have ViewModels that are bound to the Views.

我正在使用WPF应用程序并使用我自己的非常类似于M-V-VM / MVC的架构。我有每个视图的控制器,我有ViewModel绑定到视图。

For Example, I have a ToolBarView that has a corresponding ToolBarViewModel, and ToolBar Controller.

例如,我有一个ToolBarView,它有一个相应的ToolBarViewModel和ToolBar Controller。

I am using notifications to update all the views so that they do not need to reference each other, but that is not relevant for my question.

我正在使用通知来更新所有视图,以便它们不需要互相引用,但这与我的问题无关。

Each of the Views is listening for an event to trigger in their controller when the model has been updated. In the ToolBarView this looks like the following.

每个视图都在监听模型更新后在其控制器中触发的事件。在ToolBarView中,这看起来如下所示。

/*In Constructor in ToolbarView*/
Controller.Updated += UpdateView

/*Event Handler in ToolbarView*/
private void Updateview(object sender,EventArgs e)
{
    DataContext = Controller.Model;

    //Other Updating if needed

 }

If not obvious, what the above code is doing is saying that when the Controller fires the Updated event, to invoke the UpdateView(object sender,EventArgs e).

如果不明显,上面的代码正在做的是说当Controller触发Updated事件时,调用UpdateView(对象发送者,EventArgs e)。

The problem that I am experiencing is that the first time that the UpdateView() is invoked, everything is working fine, but when it is invoked the second time, the DataContext seems to still be bound to the original Controller.Model.

我遇到的问题是第一次调用UpdateView()时,一切正常,但是当第二次调用它时,DataContext似乎仍然绑定到原始的Controller.Model。

It seems almost as if I have to release the DataContext, or refresh it in order for it to be bound to the Model every time.

看起来好像我必须释放DataContext,或刷新它以便每次都绑定到Model。

The Controller is performing operations on the Model, and therefor when the UpdateView() is invoked, it needs to display the newly assigned values on that model.

Controller正在对Model执行操作,因此在调用UpdateView()时,它需要在该模型上显示新分配的值。

Is there some way I need to refresh the DataContext, or is there a different way I need to do this?

有什么方法我需要刷新DataContext,还是有不同的方式我需要这样做?

1 个解决方案

#1


10  

If you are assigning the DataContext to the same instance of your model, the in effect it won't "change". WPF expects objects to notify it when their state changes, either through DependencyProperty properties or by implementing INotifyPropertyChanged.

如果要将DataContext分配给模型的同一实例,则实际上它不会“更改”。 WPF期望对象在状态更改时通过DependencyProperty属性或通过实现INotifyPropertyChanged来通知它。

So if you do something like:

所以如果你做的事情如下:

MyObject o = new MyObject();
o.MyString = "One";
this.DataContext = o;
// ... some time later ...
o.MyString = "Two";
this.DataContext = o;

Assuming MyObject doesn't implement INotifyPropertyChanged, then the second assignment to DataContext is effectively worthless. You would have to set DataContext to null, then assign your object again to have it "refresh".

假设MyObject没有实现INotifyPropertyChanged,那么对DataContext的第二次赋值实际上毫无价值。您必须将DataContext设置为null,然后再次分配您的对象以使其“刷新”。

But your best bet in general would be to implement INotifyPropertyChanged. This would end up being much more efficient, as only the property that actually change would need to be updated in the UI.

但你最好的选择是实现INotifyPropertyChanged。这最终会更有效率,因为只需要在UI中更新实际更改的属性。

#1


10  

If you are assigning the DataContext to the same instance of your model, the in effect it won't "change". WPF expects objects to notify it when their state changes, either through DependencyProperty properties or by implementing INotifyPropertyChanged.

如果要将DataContext分配给模型的同一实例,则实际上它不会“更改”。 WPF期望对象在状态更改时通过DependencyProperty属性或通过实现INotifyPropertyChanged来通知它。

So if you do something like:

所以如果你做的事情如下:

MyObject o = new MyObject();
o.MyString = "One";
this.DataContext = o;
// ... some time later ...
o.MyString = "Two";
this.DataContext = o;

Assuming MyObject doesn't implement INotifyPropertyChanged, then the second assignment to DataContext is effectively worthless. You would have to set DataContext to null, then assign your object again to have it "refresh".

假设MyObject没有实现INotifyPropertyChanged,那么对DataContext的第二次赋值实际上毫无价值。您必须将DataContext设置为null,然后再次分配您的对象以使其“刷新”。

But your best bet in general would be to implement INotifyPropertyChanged. This would end up being much more efficient, as only the property that actually change would need to be updated in the UI.

但你最好的选择是实现INotifyPropertyChanged。这最终会更有效率,因为只需要在UI中更新实际更改的属性。