IDataErrorInfo:验证何时提交页面 [英] IDataErrorInfo: Validating when page submitted

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

问题描述

我正在创建一个WPF应用程序,它将使用我的业务对象实现的IDataErrorInfo数据验证.我一直在使用此博客文章中的演示了解ValidatesOnDataErrors.

I am creating a WPF application that will use IDataErrorInfo data validation, which my business objects implement. I have been working with this demo from a blog post to understand ValidatesOnDataErrors.

该演示是一个简单的应用程序,它将几个文本框绑定到Contact对象,并使用数据绑定中的ValidatesOnDataErrors=True来实现IDataErrorInfo验证.为使文本框有效,文本框必须至少包含五个字符.

The demo is a trivial app that binds a couple of text boxes to a Contact object and implements IDataErrorInfo validation, using ValidatesOnDataErrors=True in the data binding. To be valid, a text box has to have at least five characters.

这是我的问题:在演示中,每当文本框失去焦点时就会触发验证.实际上,在应用程序启动时,文本框将初始化为无效状态(红色边框).我的应用程序必须推迟验证,直到用户单击确定"按钮提交页面为止.那时,文本框应该经过验证,如果无效,则进入错误状态.

Here is my problem: in the demo, validation is triggered whenever a text box loses focus. In fact, the text boxes initialize to an invalid state (red borders) when the application launches. My app has to put off validation until the user clicks the OK button to submit the page. At that time, the text boxes should be validated and put into an error state if invalid.

那么,如何在用户单击确定"按钮之前将对文本框的验证推迟到?感谢您的帮助.

So, how would I defer validation on the text boxes until the user clicks the OK button? Thanks for your help.

推荐答案

数据绑定具有UpdateSourceTrigger属性.顾名思义,它指定何时更新绑定源.对于Text属性,默认情况下将其设置为LostFocus.您可以将其设置为Explicit并在代码中调用BindingExpression的UpdateSource方法.另一方面,您也可以推迟引发PropertyChanged事件.但是,我认为这些将无法解决应用程序启动时的验证问题.希望这会有所帮助.

Data binding has an UpdateSourceTrigger property. As the name implies, it specifies when to update the source of the binding. For the Text property, this is set to LostFocus by default. You could set this to Explicit and call the UpdateSource method of the BindingExpression in code. On the other hand, you could also defer the raising of the PropertyChanged event. However, these will not solve the problem of validation at application launch, I think. Hope this helps somehow.

这篇关于IDataErrorInfo:验证何时提交页面的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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