WPF 不能在TextBox类型的AddLostFocusHandler属性上设置Binding。只能在 DependencyObject 的 DependencyProperty 上设置Binding怎么回事
这是因为“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();
}
这将使用绑定表达式将文本框的值更新到绑定的属性中。
原文地址: https://www.cveoy.top/t/topic/bWpB 著作权归作者所有。请勿转载和采集!