如何验证使用 IDataErrorInfo 和 WPF 更改的另一属性?

发布于 2025-01-02 22:07:13 字数 164 浏览 4 评论 0原文

我使用 IDataErrorInfo 来验证我的视图模型,因为它允许我使用清晰的 xaml 语法,并且一旦获得它就非常简单。

我的问题是,当一个属性发生更改时如何验证另一个属性,因为验证不仅仅取决于单元格上的值,还取决于其他属性。我看到验证方法仅在属性值更改时才被调用。

提前致谢。

I'm using IDataErrorInfo to validate my viewmodels as it allows me to use a clear xaml sintax and it's pretty straightforward once you get it.

My question is how to validate one property when another one changes because the validation does not consist only on the value on the cell but depends on others. I see that the validation method is only called when the value of the property changes.

Thanks in advance.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

忱杏 2025-01-09 22:07:13

来连接它

public string this[string columnName]

对于 IDataErrorInfo,您可以通过覆盖它或编写自己的 IDataErrorInfo

。在您的情况下,请检查 columnName 是否等于您要验证的属性,然后返回一个带有表示验证错误的值的字符串,或者返回 null 表示没有错误。

另外,为了确保它得到重新验证,当依赖属性更新时,请确保为主属性执行 OnPropertyChanged

with IDataErrorInfo you can hook into

public string this[string columnName]

by overriding it, or writing your own.

in your case do a check that the columnName equals the property you are looking to validate and then either return a string with a value to represent a validation error, or null to say there was no error.

Also, to make sure it gets revalidated, when the dependent properties get updated, make sure you do a OnPropertyChanged for the main property

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文