如何从源 WPF 的丢失焦点更新目标?



我的 wpf 应用程序tb1tb2中有两个文本框。
我希望tb1.text仅在失去焦点时更新tb2.Texttb2

我试过了:

XAML

<TextBox Name="tb1" Text="{Binding Text, ElementName=tb2 }" "/>
<TextBox Name="tb2" /> 

但是tb1.Text会立即更新。

可以使用绑定来完成吗?

BindingUpdateSourceTrigger属性的默认值为LostFocus,因此如果选择tb2作为绑定目标,则可以在失去焦点时更新tb1绑定源

现在,您希望绑定以从目标到源(tb2->tb1( 的单向模式工作,因此您需要将BindingMode更改为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;
}

最新更新