Refresh DataContext for views in WPF?

I work with a WPF application and use my own architecture, which is very similar to MV-VM / MVC. I have controllers for each of the views, and I have ViewModels related to the views.

For example, I have a ToolBarView with the corresponding ToolBarViewModel and ToolBar Controller.

I use notifications to update all views, so they don’t need to refer to each other, but this does not apply to my question.

Each view listens for an event to trigger in its controller when the model is updated. In ToolBarView, it looks like this.

/*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

 }

, , , , "", UpdateView ( EventArgs e).

, , , UpdateView() , , DataContext - .Model.

, DataContext , .

, , UpdateView(), .

- , DataContext, ?

+5
1

DataContext , "". WPF , , , DependencyProperty, INotifyPropertyChanged.

, - :

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

, MyObject INotifyPropertyChanged, DataContext . DataContext null, , "".

INotifyPropertyChanged. , , .

+10

All Articles