WPF 验证 - ValidatesOnTargetUpdated

发布于 2024-11-06 04:12:40 字数 599 浏览 0 评论 0原文

我在 WPF 应用程序中有许多文本框,我在其中设置了一些验证。

加载时,应用程序会反序列化 WPF 窗口控件所绑定的类。

我有一个 ValidationRule(例如)测试文件是否存在。如果我没有设置 ValidatesOnTargetUpdated,则当我更改(绑定)文本属性时,相应的 TextBox 会进行验证,并且我会看到出现(默认)红色边框和工具提示 ({BindingrelativeSource={x:StaticRelativeSource.Self} , Path=(Validation.Errors).CurrentItem.ErrorContent}) 显示错误消息。

我希望控件在绑定时验证,而不是在属性更改时验证,因此我已将 ValidatesOnTargetUpdated 设置为 true,并且可以在调试时看到验证规则触发。

我的问题是,尽管 ValidationRule 触发,但当 ValidatesOnTargetUpdated="True" 时,(默认)ValidationTemplate 会被忽略。

我可以看到工具提示 - 它显示正确的错误消息,但边框不显示为红色;这似乎被忽略了。

有什么想法为什么会发生这种情况吗?

I have a number of textboxes in a WPF app on which I have set up some validation.

On load the app deserialises a class onto which the WPF window's controls are bound.

I have a ValidationRule that (for example) tests that a file exists. If I don't set ValidatesOnTargetUpdated then the corresponding TextBox validates when I change the (bound) Text Property and I see the (default) red border appear and a Tooltip ({Binding RelativeSource={x:Static RelativeSource.Self}, Path=(Validation.Errors).CurrentItem.ErrorContent}) displaying the error message.

I want the controls to validate on binding rather than on propertychanged so I have set the ValidatesOnTargetUpdated to true and can see the validationRule firing when debugging.

My problem is that the (default) ValidationTemplate is ignored when ValidatesOnTargetUpdated="True" despite the ValidationRule firing.

I can see the Tooltip - it appears with the correct error message but the border doesn't show as Red; this seems to be ignored.

Any ideas why this would be happening?

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

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

发布评论

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

评论(1

强辩 2024-11-13 04:12:40

您可能想阅读此论坛帖子< /a>.我相信与您的情况相关的部分是:

问题:错误模板不是
页面加载时显示

这是设计使然,因为人们可以
假设用户不想
在他/她执行之前查看错误消息
任何错误,但有时需要
这个功能。所以,
ValidatesOnTargetUpdated 属性原为
在 ValidationRule 上引入
班级;通过将其设置为 true,人们会看到
立即得到验证结果。
但是,有一个警告:您必须
确保您设置了 DataContext
页面初始化后;这
将在构造函数中
在生成的注释行之后
说初始化代码应该
去那里,或者在 Loaded 事件中。如果
您想将 DataContext 设置为
XAML,你找到了解决方案
问题在这里:
http://wpfglue.wordpress.com/2009/ 12/08/从对象导航到对象/

但是,我听说这个问题将在
WPF 4.0。

编辑:

可以在此处找到更永久的链接。

You may want to read this forum post. I believe the part that pertains to your situation is:

Problem: The Error Template is not
Displayed when a Page is Loaded

This is by design, since one could
assume that the user doesn't want to
see error messages before he/she made
any mistakes, but sometimes one needs
this functionality. So, the
ValidatesOnTargetUpdated property was
introduced on the ValidationRule
class; by setting it to true, one sees
the validation result immediately.
However, there is one caveat: you must
make sure that you set the DataContext
after the page is initialized; this
would be either in the constructor
after the generated comment line that
says that initialization code should
go there, or in the Loaded event. If
you want to set the DataContext in
XAML, you find a solution for this
problem here:
http://wpfglue.wordpress.com/2009/12/08/navigating-from-object-to-object/

However, I hear this will be fixed in
WPF 4.0.

EDIT:

A more permanent link can be found here.

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