Rebinding the DataContext of a Window in WPF
I have a Window in WPF for which i have defined a dataContext. I have a save button an on clicking the save button i am adding the updated datacontext to a collection and define a new instance of the datacontext object for the curr开发者_如何学JAVAent window. Now how do i update the window with the new DataContext.
I tried this
this.DataContext=insClassA;//Initially it is set
//Later on some event i'm updating the NewAttributeProperty
insClassA.NewAttribute = new NewAttribute();
but the UIFields still hold the previous instance's values/
Thanks.
This should take place automatically. If you set a new instance of your object to the DataContext-property of you window, all bound items should refresh automatically.
UPDATE:
Based on your edit, I assume that your object does not implement INotifyPropertyChanged and ist not a DependencyObject or the failing property is not a DependencyProperty. The problem seems to me not about the DataContext.
Your object must inform the environment, that the property has been changed. Normaly, BOs do this with the PropertyChanged-event. Search for WPF DataBinding and INotifyPropertyChanged and you will find a lot of information to this topic.
精彩评论