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

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

问题描述

只是一个简短的问题:
在 wpf 中,如何在代码隐藏中设置和获取文本框的 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.

推荐答案

TextBoxUpdateSourceTrigger是什么意思?你的意思是说TextBox.TextPropertyBindingUpdateSourceTrigger?

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

例如如果您有一个名为 myTextBoxTextBox 将其 Text 属性绑定到某个源,那么您可以轻松 get 它是 UpdateSourceTriggerBinding 对象通过 GetBindingExpression() 调用.

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);

或者,您也可以尝试分离现有的 Binding 并对其进行更新并重新分配...

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天全站免登陆