如何在代码隐藏中设置和获取文本框的 updatesourcetrigger?

编程入门 行业动态 更新时间:2024-10-28 16:19:23
本文介绍了如何在代码隐藏中设置和获取文本框的 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.

推荐答案

TextBox的UpdateSourceTrigger是什么意思?你的意思是说TextBox.TextProperty的Binding的UpdateSourceTrigger?

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

例如如果您有一个名为 myTextBox 的 TextBox 将其 Text 属性绑定到某个源,那么您可以轻松 get 它是 UpdateSourceTrigger 和 Binding 对象通过 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?

本文发布于:2023-11-09 09:17:27,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1572005.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:文本框   代码   如何在   updatesourcetrigger

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!