INotifyPropertyChanged is an interface defined in Microsoft .NET used to notify listeners of data changes made to an object. These notifications enable data-bound UI controls to update their display automatically whenever the data properties they are bound to have changed.
INotifyPropertyChanged
defines a single event property, PropertyChanged
. Classes that implement this interface are expected to fire the PropertyChanged
event whenever a property value changes. Typically this is done by adding code to the property setter of every property to fire the event whenever an assignment is made to a property.
To provide property change notification your class should derive from INotifyPropertyChanged
and provide a protected method to raise the event. The typical implementation of this method is shown below:
protected void NotifyPropertyChanged(string propertyName)
{
PropertyChangedEventHandler handler = this.PropertyChanged;
if (handler != null)
{
handler(this, new PropertyChangedEventArgs(propertyName));
}
}
This method can then be called in a property setter to notify clients that a property has changed.
public string Description
{
get { return _description; }
set
{
_description = value;
OnPropertyChanged("Description");
}
}
Property setters normally only fire the changed event if the new value is actually different than the current value of the property.
Note that the PropertyChanged
event fires after the property value has changed, and the event does not provide information about what the old value was before the change. The INotifyPropertyChanging
interface can be used to signal a property change before the change occurs.
The INotifyPropertyChanged
interface is most commonly used to facilitate data binding between your code and user interface controls in user interface technologies such as Winforms and WPF.
For more information, see the MSDN documentation for the INotifyPropertyChanged interface.