如何在代码隐藏中设置和获取文本框的更新源触发器?
只是一个简短的问题:
在 wpf 中,如何在代码隐藏中设置和获取文本框的 updatesourcetrigger ?
谢谢
更新:
我遵循 AngleWPF 的代码:
var bndExp = BindingOperations.GetBindingExpression(this, TextBox.TextProperty);
var myBinding
= bndExp.ParentBinding;
var updateSourceTrigger = myBinding.UpdateSourceTrigger;
但我得到了例外:
未处理的类型异常 “System.Reflection.TargetInitationException”发生在 PresentationFramework.dll 附加信息:已出现异常 由调用目标抛出。
Just a short question :
In wpf, how do I set and get updatesourcetrigger of a textbox in codebehind ?
Thanks
Update :
I follow AngleWPF's code :
var bndExp = BindingOperations.GetBindingExpression(this, TextBox.TextProperty);
var myBinding
= bndExp.ParentBinding;
var updateSourceTrigger = myBinding.UpdateSourceTrigger;
But I got the exception :
An unhandled exception of type
'System.Reflection.TargetInvocationException' occurred in
PresentationFramework.dll Additional information: Exception has been
thrown by the target of an invocation.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
TextBox
的UpdateSourceTrigger
是什么意思?您的意思是说TextBox.TextProperty
的Binding
的UpdateSourceTrigger
吗?例如,如果您有一个名为
myTextBox
的TextBox
,其Text
属性绑定到某个源,那么您可以轻松地获取它通过GetBindingExpression()
调用UpdateSourceTrigger
和Binding
对象。但为已使用的绑定设置
UpdateSourceTrigger
是很棘手的。例如,在上述情况下,您将无法将 myBinding.UpdateSourceTrigger 设置为其他内容。当绑定对象已在使用中时,这是不允许的。您可能必须深度克隆绑定对象并为其设置新的
UpdateSourceTrigger
,并将其分配回TextBox
。Binding
类不存在克隆。您可能必须为此编写自己的克隆代码。或者,您也可以尝试分离现有绑定并更新它,然后将其分配回来...
如果这些提示有帮助,请告诉我。
What do you mean by
UpdateSourceTrigger
ofTextBox
? You mean to sayUpdateSourceTrigger
ofTextBox.TextProperty
'sBinding
?E.g. if you have a
TextBox
namedmyTextBox
having itsText
property bound to some source then you can easily get it'sUpdateSourceTrigger
andBinding
object viaGetBindingExpression()
call.But it is tricky to set
UpdateSourceTrigger
for an already used binding. E.g. in the above case you wont be able to set themyBinding.UpdateSourceTrigger
to something else. This is not allowed when a binding object is already in use.You may have to deep clone the binding object and set new
UpdateSourceTrigger
to it and assign it back to theTextBox
. Cloning does not exist forBinding
class. You may have to write your own cloning code for the same.Alternately ou can also try to detatch the existing Binding and update it and assign it back...
Let me know if any of these tips helps.
另一种实现方法是在 TextBox 加载的事件处理程序中设置绑定。
下面是 TextBox 的 xaml:
现在,在 TxtName_OnLoaded 事件处理程序的代码后面将创建新的 Binding 对象,并根据我们的需要对其进行初始化。我们还将向其中添加 ValidationRule。
正如您在上面的实现中看到的,我们创建了一个具有新绑定路径的 Binding 对象。还将 UpdateSourceTrigger 分配给新创建的 Binding 对象。
一个绑定可以有多个验证规则。我们将在其中添加一条验证规则。现在我们可以将绑定设置为文本框的 TextProperty。
优点:您可以从代码隐藏将多个依赖项对象绑定到验证规则对象的属性,这对于 xaml 来说是不可能的。例如:
我用它来验证 TextChanged 事件的输入,在该事件中我将输入与存储为公共 ObservableCollection 属性(与 Grid 绑定)的 ApplicationViewModel 中的项目列表进行比较。 ValidationRule 的代码如下:
上面的代码接受输入并检查“Inputs”可观察集合中的可用性。如果值存在,规则给出 false ValidationResult。通过这个实现,我检查运行时输入的唯一性。
希望你们喜欢它。
A another way to implement it is setting binding in TextBox loaded eventhandler.
Below is the xaml of TextBox:
Now in code behind for TxtName_OnLoaded eventhandler will will create new Binding object and will initialize it as per our needs. Also we will add ValidationRule into it.
As you can see in above implementation we have created an object of Binding with new binding path. Also assigned UpdateSourceTrigger to newly created Binding object.
A binding can have multiple validation rules. We will add a validation rule into it. Now we can set the binding to the TextProperty of textbox.
Benefits: You can bind multiple dependency objects to the properties of Validation Rule object from code behind which is not possible with xaml. For example:
I used it to validate inputs on TextChanged event, where I compare input with the list of Items stored as public ObservableCollection property, bound with Grid, in ApplicationViewModel. Code of ValidationRule is as follows:
Above code takes input and checks availability in the "Inputs" observable collection. Rule give false ValidationResult if value exists. Through this implementation I check uniqueness of inputs on run-time.
Hope you guys have enjoyed it.
我认为这是执行此操作的正确方法:
I think this the right way to do this: