开发者

easiest way to renew Datacontext binding in WPF

开发者 https://www.devze.com 2022-12-24 04:49 出处:网络
What is the fastest way to update my DataContext binding to my WPF usercontrol so开发者_运维问答 that it shows changes in the object it is bound to in the view?The best option is to make your DataCont

What is the fastest way to update my DataContext binding to my WPF usercontrol so开发者_运维问答 that it shows changes in the object it is bound to in the view?


The best option is to make your DataContext object implement INotifyPropertyChanged. Make any collections implement INotifyCollectionChanged (ie: use an ObservableCollection<T> instead of List<T>, etc).

If you do this, then the bindings will automatically stay up to date, with no effort on your part.


Binding an ObservableCollection (which implements a specific interface) with objects that implement INotifyPropertyChanged will immediately show changes to their values in the front end or backend whenever you make changes, as long as the binding modes are set to two way binding.


This question here appears to be very similar.

Silverlight: How to force binding after setting the DataContext property

If you don't want to implement the INotifyProperty, you can use my answer and just set the datacontext again

0

精彩评论

暂无评论...
验证码 换一张
取 消