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
.
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.
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.