Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Refresh DataContext for Views in WPF?

Tags:

c#

.net

wpf

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.

For Example, I have a ToolBarView that has a corresponding ToolBarViewModel, and 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.

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

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.

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.

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.

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

like image 870
TheJediCowboy Avatar asked Mar 31 '11 21:03

TheJediCowboy


People also ask

How to set DataContext for User control in WPF?

By setting the UserControl DataContext to itself, this overwrites the DataContext and breaks Inheritance. Instead, nest it one Element deep in the XAML, in your case, the StackPanel . Put the DataContext binding here and bind it to the UserControl .

How to set DataContext in XAML in WPF?

It should be noted that the DataContext is only set after the call to InitializeComponent() in the constructor of the window class (code behind).


1 Answers

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.

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".

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.

like image 106
CodeNaked Avatar answered Sep 19 '22 18:09

CodeNaked