Introduction
Remember last time, when we figured out how to bind data with different modes here.
We used Prism's SetProperty method to update UI.
What exactly do I mean by updating UI?
We can bind properties of ViewModel with View, but is there any way to tell UI that bound properties have been modified so that the UI must update itself?
In simple words, we want to trigger UI when any bound value is modified.
To achieve this, WPF has introduced the INotifyPropertyChanged interface. It is a contract between view & viewmodel.
Let's see this in action.
Say we want to register a user & we want to calculate the user's age based on the date of birth entered by the user.
The screen will have 4 fields: User Name (TextBox), DOB (DateTimePicker), Age (TextBlock), EmailId (TextBox), Register plus Reset (Button) & response (TextBlock).
So the final window would look like this:

- <Window x:Class="A.MainWindow"
- xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
- xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
- xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
- xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
- mc:Ignorable="d"
- xmlns:ViewModel="clr-namespace:A"
- Title="MainWindow" Height="200" Width="320">
- <Window.Resources>
- <ViewModel:MainWindowViewModel x:Key="VM" ></ViewModel:MainWindowViewModel>
- <BooleanToVisibilityConverter x:Key="BooleanToVisibilityConverter"/>
- </Window.Resources>
- <Grid DataContext="{Binding Source={StaticResource VM}}"
- HorizontalAlignment="Center">
- <Grid.RowDefinitions>
- <RowDefinition Height="Auto"/>
- <RowDefinition Height="Auto"/>
- <RowDefinition Height="Auto"/>
- <RowDefinition Height="Auto"/>
- <RowDefinition Height="Auto"/>
- <RowDefinition Height="5*"/>
- </Grid.RowDefinitions>
- <Grid.ColumnDefinitions>
- <ColumnDefinition Width="Auto"/>
- <ColumnDefinition Width="Auto"/>
- </Grid.ColumnDefinitions>
- <Label x:Name="LabelUserName"
- Content="User Name:"
- Margin="0 10 0 0"/>
- <Label x:Name="LabelDOB"
- Content="DOB:"
- Grid.Row="1"/>
- <Label x:Name="LabelAge"
- Content="Age:"
- Grid.Row="2"/>
- <Label x:Name="LabelEmailId"
- Content="Email:"
- Grid.Row="3"/>
- <TextBox x:Name="TextBoxUserName"
- Text="{Binding UserName}"
- Height="20"
- Width="150"
- Margin="0 10 0 0"
- Grid.Column="1"/>
- <DatePicker x:Name="DatePickerDOB"
- SelectedDate="{Binding DOB}"
- DisplayDateStart="1/1/1990"
- Width="150"
- Grid.Column="1"
- Grid.Row="1"/>
- <Rectangle
- Stroke="LightGray"
- StrokeThickness="1"
- Height="20"
- Width="150"
- Grid.Column="1"
- Grid.Row="2"/>
- <TextBlock x:Name="TextBlockAge"
- Text="{Binding Age}"
- Height="20"
- Width="150"
- Grid.Column="1"
- Grid.Row="2"/>
- <TextBox x:Name="TextBoxEmail"
- Text="{Binding EmailId}"
- Height="20"
- Width="150"
- Grid.Column="1"
- Grid.Row="3"/>
- <StackPanel x:Name="StackPanelButtons"
- Orientation="Horizontal"
- Grid.ColumnSpan="2"
- Grid.Row="4" >
- <Button x:Name="ButtonRegister"
- Height="20"
- Width="100"
- Content="Register"
- HorizontalAlignment="Center"
- Margin="20 10 0 0"
- Command="{Binding RegisterButtonClicked}"/>
- <Button x:Name="ButtonReset"
- Height="20"
- Width="100"
- Content="Reset"
- HorizontalAlignment="Center"
- Margin="20 10 0 0"
- Command="{Binding ResetButtonClicked}"/>
- </StackPanel>
- <TextBlock x:Name="TextBlockMessage"
- HorizontalAlignment="Center"
- Margin="20 8 0 0"
- Grid.Row="5"
- Grid.ColumnSpan="2">
- <TextBlock.Style>
- <Style TargetType="TextBlock">
- <Setter Property="Text"
- Value="Enter details to register!">
- </Setter>
- <Style.Triggers>
- <DataTrigger Binding="{Binding IsButtonClicked}" Value="True">
- <Setter Property="Text"
- Value="{Binding UserName, StringFormat='User: {0} is successfully registered!'}">
- </Setter>
- </DataTrigger>
- </Style.Triggers>
- </Style>
- </TextBlock.Style>
- </TextBlock>
- </Grid>
- </Window>
Now the ViewModel: MainWindowViewModel will consist of the following:
- string UserName: User Name (TextBox)
- DateTime DOB DOB (DateTimePicker)
- string Age: Age (TextBlock)
- string EmailId: EmailId (TextBox)
- ICommand RegisterButtonClicked: Register (Button)
- ICommand ResetButtonClicked: Reset (Button)
- bool IsButtonClicked: What response to display in TextBlockMessage (TextBlock)
- ViewModel will inherit INotifyPropertyChanged interface & we will override PropertyChangedEventHandler event
- Function for calculating an age based on DOB:
Let's encapsulate all of the above:


Join the conversation! Your thoughts help the community grow.