[ACCEPTED]-Refresh DataContext for Views in WPF?-wpf

Accepted answer
Score: 13

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

So 11 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 10 doesn't implement INotifyPropertyChanged, then 9 the second assignment to DataContext is 8 effectively worthless. You would have to 7 set DataContext to null, then assign your 6 object again to have it "refresh".

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

More Related questions