这是因为 'AddLostFocusHandler' 是一个事件,而不是依赖属性。在 WPF 中,只有依赖属性才支持绑定。因此,您需要使用 'LostFocus' 事件来绑定文本框的值。例如,您可以使用以下代码:

<TextBox Text='{Binding MyText}' LostFocus='TextBox_LostFocus'/>

然后,在代码中,您可以通过以下方式处理 'LostFocus' 事件:

private void TextBox_LostFocus(object sender, RoutedEventArgs e)
{
    TextBox textBox = sender as TextBox;
    BindingExpression binding = textBox.GetBindingExpression(TextBox.TextProperty);
    binding.UpdateSource();
}

这将使用绑定表达式将文本框的值更新到绑定的属性中。

WPF TextBox 绑定 'LostFocus' 事件:解决 'AddLostFocusHandler' 绑定错误

原文地址: https://www.cveoy.top/t/topic/njeZ 著作权归作者所有。请勿转载和采集!

免费AI点我,无需注册和登录