在WPF中手动验证控件 [英] Validate control manually in WPF

查看:230
本文介绍了在WPF中手动验证控件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这里是这样的情况:
我有一个数据集绑定到WPF窗口。数据集实现IDataErrorInfo,因此当窗口中的值发生更改时,会更改特定属性的验证。
在验证期间,我可能会发现窗口中有另一个控件需要验证。
我只知道我需要验证的数据集中的字段名称,而不是绑定到此字段的实际控件。
所以我的问题是:
1.如何找到控件绑定到数据集中的特定字段?
2.如何通过代码触发该控件的验证?

Here is the situation: I have a dataset bound to a WPF window. The dataset implements the IDataErrorInfo, so when a value is changed in the window, a validation occurs for the specific property changed. During validation, I may find out there is another control in the window I need to validate. I know only the field name in the Dataset that I need to validate, and not the actual control that binds to this field. So my questions are: 1. How can I find out what control binds to a specific field in a Dataset? 2. How do I trigger validation on that control by code?

谢谢!

推荐答案

我看到的两个选项:


  1. 我认为一种处理方式是在该财产已更改,清除不再有效的财产(如果现在确实无效)。

  2. 另一个伎俩可能是在第一财产的设定者提高 PropertyChanged 事件在第二财产,强制视图重新评估它。

  1. I think one way to handle this is in the setter for the property that's changed, clear the property that's no longer valid (if it is indeed not valid now).
  2. Another trick might be in the setter of the 1st property to raise a PropertyChanged event on the 2nd property, to force the view to reevaluate it.

我会喜欢第一个选项,因为你知道改变第一个属性有机会使另一个属性无效。

I would prefer the 1st option, since you know changing the 1st property has a chance to invalidate the other property.

这篇关于在WPF中手动验证控件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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