In WPF, this is the property which gets or sets a value at the time of binding source updates. It is used in TwoWay binding. The Mode Sets the direction of the binding and Mode = TwoWay binding means that any changes occurs in target of the binding, the source is updated also and vice-versa. So that it works with only TwoWay binding, not OneWay or OneTime. UpdateSourceTrigger only applies to the source. The default value of binding is TwoWay and UpdateSourceTrigger is Default.
There are following properties available with UpdateSourceTrigger:
- Default: This is the default value for most dependency properties is PropertyChanged, while the Text property has a default value of LostFocus.
- LostFocus: It updates the binding source untill the focus moves out of the binding target element.
- PropertyChanged: It updates the binding source immediately whenever the target property changes.
- Explicit: It updates the binding source untill and unless user does it forcibly or call the UpdateSource method.
Default and LostFocus has the same meaning for most of all controls but in the case of DataGrid is
Lost Focus: Cell lost focus
Default: Row lost focus
Here is the example of UpdateSourceTrigger = Default:
<Window x:Class="WpfApplicationSample.UpdateSourceTrigger"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:local="clr-namespace:WpfApplicationSample"
mc:Ignorable="d"
Title="UpdateSourceTrigger Sample" Height="125" Width="500">
<Grid Margin="0,20,0,0">
<Grid.RowDefinitions>
<RowDefinition Height="50*" />
<RowDefinition Height="50*" />
</Grid.RowDefinitions>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="50*"/>
<ColumnDefinition Width="50*"/>
</Grid.ColumnDefinitions>
<TextBlock Grid.Row="0" Grid.Column="0" Text="Name:" Margin="0,10,0,0" Width="auto"/>
<TextBox Grid.Row="0" Grid.Column="0" Name="NameText" Margin="50,10,8,3" Width="160" />
<TextBlock Grid.Row="0" Grid.Column="1" Text="Your Name:" Margin="0,10,0,0" Width="auto"/>
<TextBox Grid.Row="0" Grid.Column="1" Name="YourNameText" Margin="60,10,8,2" Width="160" Text="{Binding ElementName=NameText,Path=Text,UpdateSourceTrigger=Default}" />
</Grid>
</Window>
In above example when you change the text of "NameText" textbox, it will show the same text in "YourNameText" textbox once you lost the focus from "NameText" texbox but vice versa steps will not reflect any changes in the "NameTextBox".
Now, look at following example for UpdateSourceTrigger = PropertyChanged :
<Window x:Class="WpfApplicationSample.UpdateSourceTrigger"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:local="clr-namespace:WpfApplicationSample"
mc:Ignorable="d"
Title="UpdateSourceTrigger Sample" Height="125" Width="500">
<Grid Margin="0,20,0,0">
<Grid.RowDefinitions>
<RowDefinition Height="50*" />
<RowDefinition Height="50*" />
</Grid.RowDefinitions>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="50*"/>
<ColumnDefinition Width="50*"/>
</Grid.ColumnDefinitions>
<TextBlock Grid.Row="0" Grid.Column="0" Text="Name:" Margin="0,10,0,0" Width="auto"/>
<TextBox Grid.Row="0" Grid.Column="0" Name="NameText" Margin="50,10,8,3" Width="160" />
<TextBlock Grid.Row="0" Grid.Column="1" Text="Your Name:" Margin="0,10,0,0" Width="auto"/>
<TextBox Grid.Row="0" Grid.Column="1" Name="YourNameText" Margin="60,10,8,2" Width="160" Text="{Binding ElementName=NameText,Path=Text,UpdateSourceTrigger=PropertyChanged}" />
</Grid>
</Window>
In above code example, changes will be copied in both textboxes when you will change text property of any textbox.
0 Comment(s)