强制将WPF文本属性的绑定设置为UpdateSourceTrigger.Explicit [英] Force binding for WPF Text property to be UpdateSourceTrigger.Explicit

查看:811
本文介绍了强制将WPF文本属性的绑定设置为UpdateSourceTrigger.Explicit的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试创建一个控件,该控件要使用BindingExpressionUpdateTarget 方法来控制绑定的更新.在控件的BeginInit中,绑定返回为空,但是在EndInit中,绑定已设置,但现在更新绑定为时已晚.我收到以下错误:绑定已使用后无法更改."以下是我用于获取绑定然后强制将UpdateSourceTrigger 设置为Explicit的代码:

I am trying to create a control that I want to control the update of the binding using the UpdateTarget method of the BindingExpression. When in the BeginInit for the control, the binding comes back null, but in the EndInit the binding has been set but it is too late to update the binding. I get the following error: "Binding cannot be changed after it has been used." the following is the code I use to get the binding and then force the UpdateSourceTrigger to be Explicit:

var binding = BindingOperations.GetBinding(this, TextProperty);
if (binding != null)
  binding.UpdateSourceTrigger = UpdateSourceTrigger.Explicit;

推荐答案

这里是使用该功能的简单示例.
Here''s a simple example using that feature.
<textbox name="itemNameTextBox">
         Text="{Binding Path=ItemName, UpdateSourceTrigger=Explicit}" /></textbox>





// itemNameTextBox is an instance of a TextBox
BindingExpression be = itemNameTextBox.GetBindingExpression(TextBox.TextProperty);
be.UpdateSource();


这篇关于强制将WPF文本属性的绑定设置为UpdateSourceTrigger.Explicit的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆