我的 wpf 应用程序tb1
和tb2
中有两个文本框。
我希望tb1.text
仅在失去焦点时更新tb2.Text
tb2
。
我试过了:
XAML
<TextBox Name="tb1" Text="{Binding Text, ElementName=tb2 }" "/>
<TextBox Name="tb2" />
但是tb1.Text
会立即更新。
可以使用绑定来完成吗?
Binding
UpdateSourceTrigger
属性的默认值为LostFocus
,因此如果选择tb2
作为绑定目标,则可以在失去焦点时更新tb1
或绑定源。
现在,您希望绑定以从目标到源(tb2->tb1( 的单向模式工作,因此您需要将Binding
的Mode
更改为OneWayToSource
。
<TextBox Name="tb1" /> <!--tb1.Text is source of binding-->
<TextBox Name="tb2" Text="{Binding Text, ElementName=tb1, Mode=OneWayToSource}"/>
如果是单向源到目标使用OneWay
,否则使用默认值,即TwoWay
。
private void tb2_LostFocus(object sender, RoutedEventArgs e)
{
tb1.Text = tb2.Text;
}