问题
What is a good way to integrate multiple field validation with IDataErrorInfo?
Let say that I have a dialog with 3 textboxes for ftp information
- URL
- Username
- Password
I have put the Required attribute on the fields (assume a normal TextBox for the password).
I validate the ftp connection when the user press "OK". At the moment I show a dialog but it would be nice if I could trigger the Validation error style on ftp connection errors.
I have looked at Validation.MarkInvalid but don't understand how to use it.
var be = GetBindingExpression(xamlURLField);
Validation.MarkInvalid(be, new ValidationError(-- WhatValidationRuleToPutHere --, be, "Can't connect to ftp", null)
回答1:
You are mixing concerns a little here. Validation is for validating user input on a basic level. Doing some post-verification should be handled differently and is generally more complex than you'd want to encompass in the area of "Validation". When something like this is hard, there is usually a reason and this is the reason.
I would treat trying to connect as a separate step in your user interaction and display a message manually.
来源:https://stackoverflow.com/questions/1838300/wpf-idataerrorinfo-multiple-field-validation