如何强制 WPF 重新验证控件?
我有一个带有业务规则的屏幕,它根据业务数据显示一些字段,但不显示其他字段。然后我使用可见性来显示/隐藏它们。
我的业务对象实现了 IDataErrorInfo。
我遇到的问题是,某些验证仅在显示字段时才适用。
在此代码摘录中,第一个 IF 仅当 type_account 为 INTERNAL 时才进行验证,
string ValidateMinimumAmount()
{
if (this.type_account != "INTERNAL")
return null;
if (this.account_minimum==null)
{
return "You must provide a minimum amount";
}
return null;
}
我遇到的问题是,由于我的 BO 的初始状态不是“Internal”,因此即使在用户选择“INTERNAL”后,验证也不会再次发生。
我如何“强制”在第一次之后进行验证。 当然,我已经检查过如果初始状态是“内部”那么它就可以工作
I have a screen with a business rules that displays some fields based on a business data and does not display others. i then play with Visibility to show/hide them.
My business object implements IDataErrorInfo.
The problem i have is that some of the validation pertains only when the field is shown.
In this code extract, the first IF makes the validation only if type_account is INTERNAL
string ValidateMinimumAmount()
{
if (this.type_account != "INTERNAL")
return null;
if (this.account_minimum==null)
{
return "You must provide a minimum amount";
}
return null;
}
the problem i have is that since the initial state of my BO is NOT "Internal" then EVEN if after the user selects "INTERNAL" the validation never occurs again.
How can i "force" the validation to occur AFTER the first time.
Of course i have checked that if the initial state is "Internal" then it works
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
我建议您通过添加错误属性来更“正确”地通知错误。
这是一篇非常容易理解的文章,它对我很有帮助,当我作为一个完全的初学者开始使用 WPF 时,您应该看一下:
http://www.codeproject.com/KB/WPF/wpfvalidation.aspx
I'd advise you to notify errors more "properly" by adding an error property.
Here is a very understandable article which helped me, when I just started working in WPF as a complete beginner, you should take a look at this :
http://www.codeproject.com/KB/WPF/wpfvalidation.aspx
你说当你玩隐藏和显示时你的“验证”消失了?但是您的带有 IDataErrorInfo 实现的对象在再次显示后仍然无效?
那么它似乎只是一个 ValidationErrorTemplate Adorner 问题。我不知道你的 xaml 是什么样子,但是用你的控件包围你的容器,以使用 AdornerDecorator 进行验证,
如果这个技巧在你的 xaml 绑定和 idataerrorinfo 实现后不能解决你的问题。
you say that your "validation" disappear when you play with hide and show? but your object with IDataErrorInfo implementet is still not valid after show again?
then it seems its just an ValidationErrorTemplate Adorner problem. i dont know how your xaml look like, but surround your container with your controls to validate with an AdornerDecorator
if this trick dont solve your problem post your xaml binding and idataerrorinfo implementation.
你的问题有点令人困惑。希望这个答案能有所帮助。
在您的示例中,如果帐户类型是内部帐户,您希望需要最低金额。这意味着
MinimumAmount
属性的验证状态取决于AccountType
的值。因此,只要AccountType
发生变化,您就必须验证MinimumAmount
。因此:如果属性之间有很多相互依赖关系,有时最简单的方法是为整个对象实现 Validate() 方法,并在任何属性更改时调用它。不要认为每次属性更改时重新验证整个对象效率低下。视图模型属性通常只会根据用户输入而更改,除非您的 Validate() 方法需要花费大量时间来运行,否则出现问题的可能性很小。
Your question is a little confusing. Hopefully this answer will help.
In your example, you want the minimum amount to be required if the account type is internal. That implies that the validation state of the
MinimumAmount
property is dependent on the value ofAccountType
. So you have to validateMinimumAmount
wheneverAccountType
changes. Thus:If you have a lot of interdependencies between properties, sometimes it's simplest to just implement a
Validate()
method for the whole object and call it whenever any property changes. Don't get hung up by the idea that it's inefficient to re-validate the entire object any time a property changes. View model properties generally only change in response to user input, and unless yourValidate()
method takes a significant amount of time to run, the odds are very small that it'll ever be a problem.