2

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

theboss
  • 69
  • 1
  • 7
  • 1
    `INotifyPropertyChanged` basically "lets the view know" that something has been changed from code, and it needs to update the visual to reflect the change. TwoWay binding means the value can be changed from either code, OR view. This link could help more: [WPF Binding One Way and Two Way](http://www.c-sharpcorner.com/blogs/wpf-binding-one-way-and-two-way1) and MSDN [Binding.Mode Property](https://msdn.microsoft.com/en-us/library/system.windows.data.binding.mode(v=vs.110).aspx) – Keyur PATEL Jun 16 '17 at 08:27
  • But in definition TwoWay **updates** the target property or the property whenever either the target property or the source property changes – theboss Jun 16 '17 at 08:43
  • Possible duplicate of [Why use INotifyPropertyChanged with bindings in WPF?](https://stackoverflow.com/questions/10475130/why-use-inotifypropertychanged-with-bindings-in-wpf) – Liam Jun 16 '17 at 09:36
  • If your property doesn't change in code and only in view where the two way binding is defined, then you don't need INotifyPropertyChanged. Sample case is a TextBox, binding will work just fine - TextBox will read initial value when DataContext is being set and will inform the property when something changed in the view. – Romasz Mar 02 '19 at 20:13

3 Answers3

1

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

Zz Oussama
  • 229
  • 1
  • 12
0

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

Waescher
  • 5,361
  • 3
  • 34
  • 51
0

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.

joe
  • 1,078
  • 2
  • 11
  • 30