前言
在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數據庫從入門到精通》