简体   繁体   中英

INotifyPropertyChanged vs Two way binding

I am newbie in c# and i want to know why we have to implement INotifyPropertyChanged interface when we use the TwoWay binding ?? and for the OneWay also ??

Thank you

In brief to support OneWay/TwoWay bindings, the underlying data must implement INotifyPropertyChanged .

Then the OneWay/TwoWay binding is just to choose the bind direction, you can find more here :

Various wpf binding modes

Implementing INotifyPropertyChanged just offers classes (others than the one implementing it) the possibility to react on property changes.

This is not possible if that interface is not implemented because if a class instance, say A sets a property on B , a third class instance C cannot track that information. Setting that property is now only a concern of A and B . If C however knows that B does implement INotifyPropertyChanged , it can simply add an event handler to the event PropertyChanged (which is part of the interface) and react on it - still hoping that B will throw the event as expected.

Bindings need that information to update the model (data) or the view depending where the change happened. So basically, they are C listening for changes of other objects ( A & B ).

INotifyPropertyChanged,like the name, is to notify your client that your property has changed, see MSDN

You will need it for updating your UI when properties change.


OneWay (Source to Target): Propertychange will cause UI update and UI operation will NOT cause propertychange. *

TwoWay (Both way): Proerty and UI are fully binded, any of them change will affect the other one.

Binding works as long as you implement INotifyPropertyChanged for your property in this case.

If you don't, nothing will change even if you use Twoway.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM