Use INotifyPropertyChanged Interface In WPF MVVM

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.

  1. namespace UseOf_INotifyPropertyChanged.Model  
  2. {  
  3.     public class User  
  4.     {  
  5.         private string _fisrtname;  
  6.         public string FirstName  
  7.         {  
  8.             get  
  9.             {  
  10.                 return _fisrtname;  
  11.             }  
  12.             set  
  13.             {  
  14.                 _fisrtname = value;  
  15.             }  
  16.         }  
  17.         private string _lastname;  
  18.         public string LastName  
  19.         {  
  20.             get  
  21.             {  
  22.                 return _lastname;  
  23.             }  
  24.             set  
  25.             {  
  26.                 _lastname = value;  
  27.             }  
  28.         }  
  29.     }  
  30. }  

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.

  1. public void RaisePropertyChange(string propertyname)  
  2.         {  
  3.             if (PropertyChanged != null)  
  4.             {  
  5.                 PropertyChanged(thisnew PropertyChangedEventArgs(propertyname));  
  6.             }  
  7.         }  

Step 6

Now, we will inherit NotifyPropertyChanged class with User class to raise property change notification.

  1. public class User : NotifyPropertyChanged  
  2.     {  
  3.         private string _fisrtname;  
  4.         public string FirstName  
  5.         {  
  6.             get  
  7.             {  
  8.                 return _fisrtname;  
  9.             }  
  10.             set  
  11.             {  
  12.                 _fisrtname = value;  
  13.                 RaisePropertyChange("FirstName");  
  14.             }  
  15.         }  
  16.         private string _lastname;  
  17.         public string LastName  
  18.         {  
  19.             get  
  20.             {  
  21.                 return _lastname;  
  22.             }  
  23.             set  
  24.             {  
  25.                 _lastname = value;  
  26.                 RaisePropertyChange("LastName");  
  27.             }  
  28.         }  
  29.     }  

Step 7

Prepare a WPF window just like below.

WPF

Step 8

Now, create a ViewModel class “MainWindowViewModel” and inherit User class in it.

public class MainWindowViewModel : User

Step 9

Declare constructor and assign values to FirstName and LastName properties.

  1. namespace UseOf_INotifyPropertyChanged.ViewModel  
  2. {  
  3.     public class MainWindowViewModel : User  
  4.     {  
  5.         public MainWindowViewModel()  
  6.         {  
  7.             FirstName = "Maulik";  
  8.             LastName = "Kansara";  
  9.         }  
  10.     }  
  11. }  

Step 10

Add Namespace for ViewModel in windows xaml file.

xmlns:VM="clr-namespace:UseOf_INotifyPropertyChanged.ViewModel"

Step 11

Now set MainWindowViewModel as window’s DataContext.

  1. <Window.DataContext>  
  2.         <VM:MainWindowViewModel x:Name="VMMainWindow"></VM:MainWindowViewModel>  
  3.     </Window.DataContext>  

Step 12

Bind text boxes’ text property to respective proper model Property.

  1. <TextBox Grid.Row="1" Grid.Column="1" Height="25" Text="{Binding FirstName}" Margin="2"></TextBox>  
  2. <TextBox Grid.Row="2" Grid.Column="1" Height="25" Text="{Binding LastName}" Margin="2"></TextBox>  

Step 13

Now, the complete XAML file will look like this.

  1. <Window x:Class="UseOf_INotifyPropertyChanged.MainWindow"  
  2.         xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"  
  3.         xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"  
  4.         xmlns:d="http://schemas.microsoft.com/expression/blend/2008"  
  5.         xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"  
  6.         xmlns:local="clr-namespace:UseOf_INotifyPropertyChanged"  
  7.         mc:Ignorable="d"  
  8.         xmlns:VM="clr-namespace:UseOf_INotifyPropertyChanged.ViewModel"  
  9.         Title="MainWindow" Height="200" Width="300">  
  10.     <Window.DataContext>  
  11.         <VM:MainWindowViewModel x:Name="VMMainWindow"></VM:MainWindowViewModel>  
  12.     </Window.DataContext>  
  13.     <Grid>  
  14.         <Grid.ColumnDefinitions>  
  15.             <ColumnDefinition Width="100"/>  
  16.             <ColumnDefinition Width="*"/>  
  17.             <ColumnDefinition Width="20"/>  
  18.         </Grid.ColumnDefinitions>  
  19.         <Grid.RowDefinitions>  
  20.             <RowDefinition Height="25"/>  
  21.             <RowDefinition Height="30"/>  
  22.             <RowDefinition Height="30"/>  
  23.             <RowDefinition Height="*"/>  
  24.         </Grid.RowDefinitions>  
  25.         <Label Content="First Name :" Grid.Column="0" Grid.Row="1" HorizontalAlignment="Right" VerticalAlignment="Center"></Label>  
  26.         <Label Content="Last Name :" Grid.Column="0" Grid.Row="2" HorizontalAlignment="Right" VerticalAlignment="Center"></Label>  
  27.         <TextBox Grid.Row="1" Grid.Column="1" Height="25" Text="{Binding FirstName}" Margin="2"></TextBox>  
  28.         <TextBox Grid.Row="2" Grid.Column="1" Height="25" Text="{Binding LastName}" Margin="2"></TextBox>  
  29.     </Grid>  
  30. </Window>  

Step14

Run the application and you will observe that values assigned to properties FirstName and LastName are reflected in their respective textboxes.

WPF