INotifyPropertyChanged is an interface member in System.ComponentModel Namespace. This interface is used to notify the Control that the property value has changed.
The following example will demonstrate how to implement the INotifyPropertyChanged interface.
Scenario
Let us consider there are two fields - First Name and Last Name. Now, we will implement a logic that when we update the value for First Name and Last Name property programmatically, it will be reflected automatically in the UI.
Step 1
Create one model class “User” containing two properties respectively, for First Name and Last Name.
namespace UseOf_INotifyPropertyChanged.Model
{
public class User
{
private string _fisrtname;
public string FirstName
{
get
{
return _fisrtname;
}
set
{
_fisrtname = value;
}
}
private string _lastname;
public string LastName
{
get
{
return _lastname;
}
set
{
_lastname = value;
}
}
}
}
Step 2
Create one class “NotifyPropertyChanged” to implement INotifyPropertyChanged interface. Here, we are creating one separate class so that we can inherit this class in all classes where we want to implement the property change notification.
Step 3
Inherit INotifyPropertyChanged interface with NotifyPropertyChanged class.
public class NotifyPropertyChanged : INotifyPropertyChanged
Step 4
On Inherit, an event will be created automatically
public event PropertyChangedEventHandler PropertyChanged;
Step 5
Now, we have to call this event to notify change in the property to UI. Therefore, we will implement a method to call this event.
public void RaisePropertyChange(string propertyname)
{
if (PropertyChanged != null)
{
PropertyChanged(this, new PropertyChangedEventArgs(propertyname));
}
}
Step 6
Now, we will inherit NotifyPropertyChanged class with User class to raise property change notification.
public class User : NotifyPropertyChanged
{
private string _fisrtname;
public string FirstName
{
get
{
return _fisrtname;
}
set
{
_fisrtname = value;
RaisePropertyChange("FirstName");
}
}
private string _lastname;
public string LastName
{
get
{
return _lastname;
}
set
{
_lastname = value;
RaisePropertyChange("LastName");
}
}
}
Step 7



Join the conversation! Your thoughts help the community grow.