Validation empty fields in MVVM
I have a XAML with about 100 controls. All of them are bound to class Student.
I can validate such pool like Age, or Name, but how to validate controls,which user not even touch ?
For example for field Address.
Address couldn't be null, but user forgot about fil开发者_C百科ling this control.
There is something called ValidationRule
in WPF. You should try googling for this or if you got the Book "WPF Unleashed" look after page 298.
Set ValidatesOnTargetUpdated to true on your ValidationRule and it will run when the binding is initialized instead of only when the user modifies the data.
You could also have your Student class implement IDataErrorInfo and set ValidatesOnDataErrors=True on your Bindings. This uses DataErrorValidationRule, which has ValidatesOnTargetUpdated set.
There is a cool library called FluentValidation, http://fluentvalidation.codeplex.com/, it has a bunch of ValidationRules built in and makes it really easy to implement new ones. This was intended as a comment, but alas I have not enough points...
精彩评论