Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What's better? INotifyPropertyChanged or having separate *Changed events?

Tags:

c#

properties

I'm designing a new class in C# which has a few properties. My users will want to know when each of them changes.

What's a better choice? INotifyPropertyChanged style of implementation, or just having separate events corresponding to my properties? Or both?

like image 310
Filip Frącz Avatar asked Jun 08 '09 15:06

Filip Frącz


People also ask

What is the purpose of INotifyPropertyChanged?

The INotifyPropertyChanged interface is used to notify clients, typically binding clients, that a property value has changed. For example, consider a Person object with a property called FirstName .

How do you implement INotifyPropertyChanged?

To implement INotifyPropertyChanged you need to declare the PropertyChanged event and create the OnPropertyChanged method. Then for each property you want change notifications for, you call OnPropertyChanged whenever the property is updated.

What is INotifyPropertyChanged xamarin?

The INotifyPropertyChanged changed interface is at the heart of XAML apps and has been a part of the . NET ecosystem since the early days of Windows Forms. The PropertyChanged event notifies the UI that a property in the binding source (usually the ViewModel) has changed. It allows the UI to update accordingly.

What is OnPropertyChanged C#?

INotifyPropertyChanged is an interface member in System. ComponentModel Namespace. This interface is used to notify the Control that property value has changed. Sourcecode.zip.


2 Answers

Going forward, INotifyPropertyChanged is the norm, and has much better support in WPF. I seem to recall that BindingList<T> only respects INotifyPropertyChanged (see HookPropertyChanged and UnhookPropertyChanged in reflector).

This is also more efficient, as the UI only needs one event hook, rather than one per event - and your class can be more efficient as it only needs a field for one handler (rather than either one per property, or the niggle of having to go via EventHandlerList and a set of static keys)

The old style is mainly a hangover.

like image 197
Marc Gravell Avatar answered Oct 05 '22 22:10

Marc Gravell


Implementing the INotifyPropertyChanged interface will give you the added benefit of having binding sources automatically listening to changes you make to your properties and updating the controls.

Try doing this. Create a class without the INotifyPropertyChanged interface and bind it to something. For instance you can bind one of its properties to the Text property of a TextBox. Add a button that will change, not the text of the TextBox, but the value of the respective property in the instance that is bound to the box. Run and click the button. The textbox will not be notified of the change. If you then implement the INotifyPropertyChanged in the class, have the property's setter notify of its change via the PropertyChanged even, after you repeat the experiment, you'll see the TextBox updating.

like image 24
Rui Craveiro Avatar answered Oct 05 '22 22:10

Rui Craveiro