wpf的Binding之UpdateSourceTrigger
前言
在wpf界面开发中,Binding的源和目标之间可以通过Mode来决定数据的传递方向,同时数据传递时的触发条件也是可以有多种情况,多种情况由UpdateSourceTrigger属性来控制,该属性有Default、Explicit、LostFocus、PropertyChanged四种情况,本文就来详细讲解:
1、Default
在下面的xaml代码中,定义了两个TextBox,一个Button,由于Binding中Mode设置为OneWayToSource,所以数据传递是由目标tbx_Target传向tbx_test。
<Window x:Class="控件作为Binding的源.MainWindow"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:控件作为Binding的源"mc:Ignorable="d"Title="MainWindow" Height="450" Width="800"><StackPanel><TextBox x:Name="tbx_test" Height="100" Background="Red" /><TextBox x:Name="tbx_Target" Height="100" Background="Green" Text="{Binding Path= Text , ElementName=tbx_test,Mode=OneWayToSource, UpdateSourceTrigger=Default}" /><Button Height="100" Background="Red" Click="Button_Click" /></StackPanel>
</Window>
软件运行以后,发现在tbx_Target输入123,tbx_test的Text属性没有立即更新,这是因为此时UpdateSourceTrigger=Default,这个Default代表的就是tbx_Target失去焦点以后才会触发数据更新,所以当我们点击最下方的Button按钮以后,会发现123成功更新到最上方的tbx_test。
点击最下方的按钮后的界面
2、LostFocus
由于UpdateSourceTrigger属性设置为Default相当于失去焦点,这个失去焦点其实和LostFocus是同样的功能,所以就不多做介绍。
4、PropertyChanged
这个值就是属性改变就生效,在下面的代码中,设置UpdateSourceTrigger=PropertyChanged以后,当改变tbx_Target的值的时候,tbx_test立马也会更新,这是由于此时数据更新的条件变成了属性更改,当改变tbx_Target的Test值的时候就相当于属性更改所以立马触发数据更新,这个属性用于实时触发情况。
<Window x:Class="控件作为Binding的源.MainWindow"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:控件作为Binding的源"mc:Ignorable="d"Title="MainWindow" Height="450" Width="800"><StackPanel><TextBox x:Name="tbx_test" Height="100" Background="Red" /><TextBox x:Name="tbx_Target" Height="100" Background="Green" Text="{Binding Path= Text , ElementName=tbx_test,Mode=OneWayToSource, UpdateSourceTrigger=PropertyChanged}" /><Button Height="100" Background="Red" Click="Button_Click" /></StackPanel>
</Window>
5、Explicit
这个值相当于手动触发更新,也就是单纯的更改Binding目标的值并不会触发源更新。只有通过一些代码强制更新,代码如下:
<Window x:Class="控件作为Binding的源.MainWindow"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:控件作为Binding的源"mc:Ignorable="d"Title="MainWindow" Height="450" Width="800"><StackPanel><TextBox x:Name="tbx_test" Height="100" Background="Red" /><TextBox x:Name="tbx_Target" Height="100" Background="Green" Text="{Binding Path= Text , ElementName=tbx_test,Mode=OneWayToSource, UpdateSourceTrigger=PropertyChanged}" /><Button Height="100" Background="Red" Click="Button_Click" /></StackPanel>
</Window>
private void Button_Click(object sender, RoutedEventArgs e){BindingExpression be = tbx_Target.GetBindingExpression(TextBox.TextProperty);be.UpdateSource();//将目标值发送到源}
上面的xaml代码中未Button注册了一个单击事件,事件的内容中有两行代码,第一行获取Binding目标的BindingExpression对象,第二行代码调用UpdateSource将目标的值发送到源,起到了手动强制更新数据的功能。
马工撰写的年入30万+C#上位机项目实战必备教程(点击下方链接即可访问文章目录)
1、《C#串口通信从入门到精通》
2、《C#与PLC通信从入门到精通 》
3、《C# Modbus通信从入门到精通》
4、《C#Socket通信从入门到精通 》
5、《C# MES通信从入门到精通》
6、《winform控件从入门到精通》
7、《C#操作MySql数据库从入门到精通》