Pages

Monday, August 8, 2011

Validation Rules in WPF

WPF data binding model provides ability to validate user input. You can associate Validation Rule with your Binding as well with MultiBinding. Binding engine automatically checks whether any ValidationRule associated or not if yes then every time value passes to the source it will applies validation. Validation applies only when target value updates source value (i.e. with TwoWay or OneWayToSource binding modes).

To create validation rule you need to inherit ValidationRule class. This class is abstract class and has abstract method named Validate which returns ValidationResult type.

In above example, Name and Age textbox are bound with Name and Age property of Employee class. Name property implements NameValidationRule and Age property implements two validation rules first one is OnlyDigitsValidationRule and second AgeRangeValidationRule.

NameValidationRule allows only alphabets and space it means you can’t enter other than alphabets inside Name textbox. While AgeRangeValidationRule allows age between 18 to 35 and OnlyDigitsValidationRule allows only digits to Age textbox.

Age property implements two validation rules so when multiple validation rules are associated with binding then they are validated in sequence means as per the association order. As per above example OnlyDigitsValidationRule validates first and followed by AgeRangeValidationRule.

Your information about asp is really interesting and innovative. Also I want you to share latest updates about asp. Can you update it in your website? Thanks for sharingDot net training institute in Chennai