UpdateSourceTrigger in WPF

It is one of the property of Data Binding. As the name suggests that it defines timings when should the source will reflect changes if the target gets updated. So we have two modes which updates the source. They are TwoWay and OneWayToSource

We have four properties of UpdateSourceTrigger

  • Default
  • LostFocus
  • PropertyChanged
  • Explicit

By setting UpdateSourceTrigger equal to one of these properties, we define when the source should reflect the changes.

Let us understand them one by one.

1. Default :

It is the default value which varies from control to control.
Example- Textbox have ‘LostFocus’ by Default and Checkbox have ‘PropertyChanged’.

2. LostFocus :

This means that the source will update when the focus from the target is Lost.

3. PropertyChanged :

It clearly specifies that it updates the source as soon as the target gets updated. Means as you type any character in target, immediately the source reflects.



4. Explicit :

It tells that we have to do something explicitly to tell the source about the updation of target. So for that purpose we have to call UpdateSource() method of BindingExpression. The definition of this function goes into your code-behind file.

You can call this method on Button Click.

Let us take an example which demonstrates all of the above properties.

XAML File

<Window.Resources>
<Style x:Key=”txtStyle” TargetType=”{x:Type TextBox}”>
<Setter Property=”Width” Value=”100″ />
<Setter Property=”Height” Value=”30″ />
<Setter Property=”HorizontalAlignment” Value=”Center” />
<Setter Property=”VerticalAlignment” Value=”Center” />
</Style>
</Window.Resources>
<Grid Margin=”10,10″ Background=”#98dafc”>
<Grid.ColumnDefinitions>
<ColumnDefinition Width=”0.6*” />
<ColumnDefinition Width=”*”/>
<ColumnDefinition Width=”*” />
</Grid.ColumnDefinitions>
<Grid.RowDefinitions>
<RowDefinition Height=”*” />
<RowDefinition Height=”*” />
<RowDefinition Height=”*” />
<RowDefinition Height=”*” />
<RowDefinition Height=”*” />
<RowDefinition Height=”*” />
</Grid.RowDefinitions>
<TextBlock Text=”UpdateSourceTrigger” FontSize=”25″ FontWeight=”Bold” Grid.ColumnSpan=”3″ HorizontalAlignment=”Center” VerticalAlignment=”Center”/>

<TextBlock Text=”Types” FontSize=”23″ Grid.Row=”1″ Grid.Column=”0″ HorizontalAlignment=”Center” VerticalAlignment=”Center”/>
<TextBlock Text=”Source” FontSize=”23″ Grid.Row=”1″ Grid.Column=”1″ HorizontalAlignment=”Center” VerticalAlignment=”Center”/>
<TextBlock Text=”Target” FontSize=”23″ Grid.Row=”1″ Grid.Column=”2″ HorizontalAlignment=”Center” VerticalAlignment=”Center” />

<TextBlock Text=”LostFocus” Grid.Row=”2″ Grid.Column=”0″ HorizontalAlignment=”Center” VerticalAlignment=”Center”/>
<TextBox x:Name=”txt1″ Style=”{StaticResource txtStyle}” Grid.Row=”2″ Grid.Column=”1″ />
<TextBox x:Name=”txt2″ Style=”{StaticResource txtStyle}” Text=”{Binding ElementName=txt1,Path=Text,Mode=TwoWay,UpdateSourceTrigger=LostFocus}” Grid.Row=”2″ Grid.Column=”2″ />

<TextBlock Text=”PropertyChanged” Grid.Row=”3″ Grid.Column=”0″ HorizontalAlignment=”Center” VerticalAlignment=”Center”/>
<TextBox x:Name=”txt3″ Style=”{StaticResource txtStyle}” Grid.Row=”3″ Grid.Column=”1″ />
<TextBox x:Name=”txt4″ Style=”{StaticResource txtStyle}” Text=”{Binding ElementName=txt3, Path=Text, Mode=TwoWay, UpdateSourceTrigger= PropertyChanged}” Grid.Row=”3″ Grid.Column=”2″ />

<TextBlock Text=”Explicit” Grid.Row=”4″ HorizontalAlignment=”Center” VerticalAlignment=”Center” />
<TextBox x:Name=”txt5″ Text=”{Binding ElementName=txt6,Path=Text,UpdateSourceTrigger=Explicit}” Style=”{StaticResource txtStyle}” Grid.Row=”4″ Grid.Column=”1″ />
<Button x:Name=”btnExplicit” Width=”50″ Height=”25″ Click=”btnExplicit_Click” Grid.Row=”4″ Grid.Column=”1″ Grid.ColumnSpan=”2″>Explict</Button>
<TextBox x:Name=”txt6″ Style=”{StaticResource txtStyle}” Grid.Row=”4″ Grid.Column=”2″ Margin=”20,6,21,6″ />

<TextBlock Text=”Default – This is the default value and it means a lost focus for most of the controls” Grid.Row=”5″ Grid.ColumnSpan=”3″ HorizontalAlignment=”Center” VerticalAlignment=”Center” />
</Grid>

.CS File

private void btnExplicit_Click(object sender, RoutedEventArgs e)
{
BindingExpression binding = txt5.GetBindingExpression(TextBox.TextProperty);
binding.UpdateSource();
}

updatesourcetrigger