WPF验证-ValidatesOnTargetUpdated [英] WPF validation - ValidatesOnTargetUpdated

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

问题描述

我在WPF应用程序中有多个文本框,并在其中设置了一些验证功能.

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

在加载时,应用反序列化WPF窗口控件绑定到的类.

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

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

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.

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

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.

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

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?

推荐答案

您可能想阅读以下内容

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

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

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/

不过,我听说这将在 WPF 4.0.

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

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

A more permanent link can be found here.

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

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