There seems to be four ways to go with validation with WPF:
- Use IDataErrorInfo
- Use custom classes with ValidationRule
- Combine 1 and 2
- Roll your own
What do I mean by each of these, and why?
- Use IDataErrorInfo. You canimplement this interface in your business objects and you get the opportunity to specify validation for each property or for all properties. You do this in (usually) a general purpose method that does validation for all of your properties in your business object. This is good if you need validation for a business object, but not across objects. You can tell wpf to use it (in v3.5) by specifying ValidatesOnDataErrors=”True” in your binding specification.
- Inherit a class from ValidationRule, and provide an override for the Validate method. You could do this for either a particular business object, or across a number of objects. You can use it in xaml by specifying it in the binding’s ValidationRules collection.
- Do 1 for business object validation, and 2 for any validation that needs to be done across business objects
- Do this when you need to extend the validation, e.g. like in the CSLA.Net framework