Explain INotifyPropertyChanged In WPF - MVVM

INotifyPropertyChanged interface is used to notify the view or ViewModel that it does not matter which property is binding; it is updated.

Let's take an example for understanding this interface. Take one WPF Window in which there are a total of  three fields: First Name, Last Name and Full Name. Here, first name and last name text boxes are editable. So, based on the change in first name and last name, we have to automatically update the full name.

  1. Make window design like,

    window

  2. XAML Code for WPF Window is given below-
    1. <Window x:Class="MVVM_INotifyPropertyChanged.MainWindow" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" Title="MainWindow">  
    2.     <Grid Width="400" Height="Auto" HorizontalAlignment="Center" VerticalAlignment="Stretch" Margin="20">  
    3.         <Grid.RowDefinitions>  
    4.             <RowDefinition Height="40" />  
    5.             <RowDefinition Height="40" />  
    6.             <RowDefinition Height="40" /> </Grid.RowDefinitions>  
    7.         <Grid.ColumnDefinitions>  
    8.             <ColumnDefinition Width="90" />  
    9.             <ColumnDefinition/> </Grid.ColumnDefinitions>  
    10.         <Label Grid.Row="0" Grid.Column="0" Content="First Name : "></Label>  
    11.         <Label Grid.Row="1" Grid.Column="0" Content="Last Name : "></Label>  
    12.         <Label Grid.Row="2" Grid.Column="0" Content="Full Name : "></Label>  
    13.         <TextBox Grid.Row="0" Grid.Column="1"></TextBox>  
    14.         <TextBox Grid.Row="1" Grid.Column="1"></TextBox>  
    15.         <TextBox Grid.Row="2" Grid.Column="1"></TextBox>  
    16.     </Grid>  
    17. </Window>  
  3. Now, we create one model, which contains one class call person and it has 3 properties “FirstName”,”LastName”,”FullName”.
    1. public class Person {  
    2.     private string _fisrtname;  
    3.     public string FirstName {  
    4.         get {  
    5.             return _fisrtname;  
    6.         }  
    7.         set {  
    8.             _fisrtname = value;  
    9.         }  
    10.     }  
    11.     private string _lastname;  
    12.     public string LastName {  
    13.         get {  
    14.             return _lastname;  
    15.         }  
    16.         set {  
    17.             _lastname = value;  
    18.         }  
    19.     }  
    20.     private string _fullname;  
    21.     public string FullName {  
    22.         get {  
    23.             return _fullname;  
    24.         }  
    25.         set {  
    26.             _fullname = value;  
    27.         }  
    28.     }  
    29.     public Person() {  
    30.         _fisrtname = "Nirav";  
    31.         _lastname = "Daraniya";  
    32.     }  
    33. }  
  4. Now our actual part is started. We implement the interface on “Person” class so it automatically creates one event
    1. public event PropertyChangedEventHandler PropertyChanged;  
  5. Now, we have to call this event, when any property of class is changed. Thus, we create one method in which this event is called. in this method, we first check if event is null or not, if not, we proceed.
    1. private void OnPropertyRaised(string propertyname) {  
    2.     if (PropertyChanged != null) {}  
    3. }  
  6. Now, we run this event by passing the property name. 
    1. public event PropertyChangedEventHandler PropertyChanged;  
    2. private void OnPropertyRaised(string propertyname) {  
    3.     if (PropertyChanged != null) {  
    4.         PropertyChanged(thisnew PropertyChangedEventArgs(propertyname));  
    5.     }  
    6. }  
  7. Now, we call this method on all the “Set” section of the property with the property name like-
    1. OnPropertyRaised("FirstName");  
    2. OnPropertyRaised("LastName");  
    3. OnPropertyRaised("FullName");  
    Here, we call the Full Name property change on both, if the first name changes or last name changes, as shown below-

  8. Now, full Person class will look like-
    1. public class Person: INotifyPropertyChanged {  
    2.     private string _fisrtname;  
    3.     public string FirstName {  
    4.         get {  
    5.             return _fisrtname;  
    6.         }  
    7.         set {  
    8.             _fisrtname = value;  
    9.             OnPropertyRaised("FirstName");  
    10.             OnPropertyRaised("FullName");  
    11.         }  
    12.     }  
    13.     private string _lastname;  
    14.     public string LastName {  
    15.         get {  
    16.             return _lastname;  
    17.         }  
    18.         set {  
    19.             _lastname = value;  
    20.             OnPropertyRaised("LastName");  
    21.             OnPropertyRaised("FullName");  
    22.         }  
    23.     }  
    24.     private string _fullname;  
    25.     public string FullName {  
    26.         get {  
    27.             return _fullname;  
    28.         }  
    29.         set {  
    30.             _fullname = value;  
    31.             OnPropertyRaised("FullName");  
    32.         }  
    33.     }  
    34.     public Person() {  
    35.         _fisrtname = "Nirav";  
    36.         _lastname = "Daraniya";  
    37.     }  
    38.     public event PropertyChangedEventHandler PropertyChanged;  
    39.     private void OnPropertyRaised(string propertyname) {  
    40.         if (PropertyChanged != null) {  
    41.             PropertyChanged(thisnew PropertyChangedEventArgs(propertyname));  
    42.         }  
    43.     }  
    44. }  
  9. Now, we bind this property to the view. First, we build the solution and add this model namespace to the view, as shown below-
    1. xmlns:model="clr-namespace:MVVM_INotifyPropertyChanged.Model"  
  10. Now, we add this model to Window resource file.
    1. <Window.Resources>  
    2.    <model:Person x:Key="m" ></model:Person>  
    3. </Window.Resources>  
  11. Now, set the data context for the grid.
    1. DataContext="{Binding Source={StaticResource m}}"  
  12. Now, we bind the Text property for the all the three textboxes, as show below. For the First Name text box, use-
    1. Text="{Binding Path=FirstName}"  
  13. Now, the full code for XAML file is given below-
    1. <Window x:Class="MVVM_INotifyPropertyChanged.MainWindow"  
    2.         xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"  
    3.         xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"  
    4.         xmlns:model="clr-namespace:MVVM_INotifyPropertyChanged.Model"  
    5.         Title="MainWindow">  
    6.     <Window.Resources>  
    7.         <model:Person x:Key="m" ></model:Person>  
    8.     </Window.Resources>  
    9.      
    10.     <Grid DataContext="{Binding Source={StaticResource m}}"  
    11.         Width="400" Height="Auto" HorizontalAlignment="Center" VerticalAlignment="Stretch" Margin="20">  
    12.         <Grid.RowDefinitions>  
    13.             <RowDefinition Height="40"/>  
    14.             <RowDefinition Height="40"/>  
    15.             <RowDefinition Height="40"/>  
    16.         </Grid.RowDefinitions>  
    17.   
    18.         <Grid.ColumnDefinitions>  
    19.             <ColumnDefinition Width="90"/>  
    20.             <ColumnDefinition/>  
    21.         </Grid.ColumnDefinitions>  
    22.         <Label Grid.Row="0" Grid.Column="0" Content="First Name : "></Label>  
    23.         <Label Grid.Row="1" Grid.Column="0" Content="Last Name : "></Label>  
    24.         <Label Grid.Row="2" Grid.Column="0" Content="Full Name : "></Label>  
    25.         <TextBox Grid.Row="0" Grid.Column="1" Text="{Binding Path=FirstName} , UpdateSourceTrigger=PropertyChanged"></TextBox>  
    26.         <TextBox Grid.Row="1" Grid.Column="1" Text="{Binding Path=LastName} , UpdateSourceTrigger=PropertyChanged"></TextBox>  
    27.         <TextBox Grid.Row="2" Grid.Column="1" Text="{Binding Path=FullName} , UpdateSourceTrigger=PropertyChanged"></TextBox>  
    28.     </Grid>  
    29.   
    30. </Window>  
  14. Now, run the Application and check the result, change in first name and last name text box. Now, you can show that full name will automatically change, as per first name and last name.

    window