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

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

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

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

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?

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

发布评论

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

>www.elefans.com

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