theboss
theboss

Reputation: 69

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

Upvotes: 2

Views: 3418

Answers (3)

Zz Oussama
Zz Oussama

Reputation: 229

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

Upvotes: 1

Waescher
Waescher

Reputation: 5747

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 Chowever 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).

Upvotes: 0

joe
joe

Reputation: 1308

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.

Upvotes: 0

Related Questions