INotifyPropertyChanged Interface in WPF

INotifyPropertyChanged Interface is used to notifies the client or your class that one of its property has been changed. And it contains only event ‘PropertyChanged‘ of delegate type ‘PropertyChangedEventHandler‘ which updates that property’s value through its set accessor.

Namespace : System.ComponentModel
Assembly : System (in System.dll)




How INotifyPropertyChanged Interface looks like

// Notifies clients that a property value has changed.
public interface INotifyPropertyChanged
{
// Occurs when a property value changes.
event PropertyChangedEventHandler PropertyChanged;
}

Now Let us take an example.
We have a textbox which is bind to a property ‘Name’ of class ViewModel.

XAML CODE

<TextBox x:Name=”txt1″ Text=”{Binding Path=Name,Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}” />

.CS CODE

public class ViewModel : INotifyPropertyChanged
{
private string name;
public string Name
{
get { return name; }
set
{
name = value;
if (name != value)
OnPropertyChanged(“Name”);
}
}

public event PropertyChangedEventHandler PropertyChanged;
public void OnPropertyChanged(string propertyName)
{
if(PropertyChanged != null)
PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
}
}