如何在代码隐藏中设置和获取文本框的updatesourcetrigger? [英] How to set and get updatesourcetrigger of a textbox in codebehind?

查看:147
本文介绍了如何在代码隐藏中设置和获取文本框的updatesourcetrigger?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

一个简短的问题:
在wpf中,如何在codebehind中设置并获取文本框的updatesourcetrigger?
谢谢

Just a short question :
In wpf, how do I set and get updatesourcetrigger of a textbox in codebehind ?
Thanks

更新:
我遵循AngleWPF的代码:

Update :
I follow AngleWPF's code :

        var bndExp = BindingOperations.GetBindingExpression(this, TextBox.TextProperty);

        var myBinding
          = bndExp.ParentBinding;

        var updateSourceTrigger = myBinding.UpdateSourceTrigger;

但是我有一个例外:

类型的未处理异常 "System.Reflection.TargetInvocationException"发生在 PresentationFramework.dll附加信息:异常已发生 由调用目标抛出.

An unhandled exception of type 'System.Reflection.TargetInvocationException' occurred in PresentationFramework.dll Additional information: Exception has been thrown by the target of an invocation.

推荐答案

TextBox中的UpdateSourceTrigger是什么意思?您是说TextBox.TextPropertyBinding中的UpdateSourceTrigger吗?

What do you mean by UpdateSourceTrigger of TextBox? You mean to say UpdateSourceTrigger of TextBox.TextProperty's Binding?

例如如果您有一个TextBox名为myTextBox且其Text属性已绑定到某些源,则可以通过GetBindingExpression()调用轻松获取它是UpdateSourceTriggerBinding对象.

E.g. if you have a TextBox named myTextBox having its Text property bound to some source then you can easily get it's UpdateSourceTrigger and Binding object via GetBindingExpression() call.

   var bndExp
     = BindingOperations.GetBindingExpression(myTextBox, TextBox.Textproperty);

   var myBinding
     = bndExp.ParentBinding; 

   var updateSourceTrigger
     = myBinding.UpdateSourceTrigger;

但是设置 UpdateSourceTrigger对于已经使用的绑定是很棘手的.例如.在上述情况下,您将无法将myBinding.UpdateSourceTrigger设置为其他设置.当绑定对象已经在使用中时,这是不允许的.

But it is tricky to set UpdateSourceTrigger for an already used binding. E.g. in the above case you wont be able to set the myBinding.UpdateSourceTrigger to something else. This is not allowed when a binding object is already in use.

您可能必须深克隆,并将绑定对象设置为新的UpdateSourceTrigger,然后将其分配回TextBox. Binding类不存在克隆.您可能必须为此编写自己的克隆代码.

You may have to deep clone the binding object and set new UpdateSourceTrigger to it and assign it back to the TextBox. Cloning does not exist for Binding class. You may have to write your own cloning code for the same.

  var newBinding = Clone(myBinding); //// <--- You may have to code this function.
  newBinding.UpdateSourceTrigger = UpdateSourceTrigger.Explicit;
  myTextBox.SetBinding(TextBox.TextProperty, newBinding);

或者,您也可以尝试释放现有的绑定并进行更新并将其分配回...

Alternately ou can also try to detatch the existing Binding and update it and assign it back...

  myTextBox.SetBinding(TextBox.TextProperty, null);
  myBinding.UpdateSourceTrigger = UpdateSourceTrigger.Explicit;
  myTextBox.SetBinding(TextBox.TextProperty, myBinding);

让我知道这些提示是否有帮助.

Let me know if any of these tips helps.

这篇关于如何在代码隐藏中设置和获取文本框的updatesourcetrigger?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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