Data Validation is fully implemented in Silverlight 3, utilizing the object the input control is bound to as the decider of what is valid, and the Visual State Machine to provide feedback to the user.
The process is best understood as a sequence of steps,
1. Add an input control to your page.
2. Bind the input control to a data object using two way binding. Set both ValidatesOnException and NotifyOnValidationError to true on the data object
3. Test the data for validity and throw an exception for invalid data in any of the following three places:
? Binding Type Converter
? Data bound object’s set accessor for the corresponding property
? validation attribute applied to the data object or member
If you throw an exception put a meaningful message into the exception’s message property
For example, the data object that a registration form is bound to might have a Password2 property that tests to make sure the user has entered the same password twice:
1: public string Password2
2: {
3: get { return pw2; }
4: set
5: {
6: if ( value != Password1)
7: {
8: throw new ArgumentException( "Passwords must match. ");
9: }
10: pw2 = value;
11: NotifyPropetyChanged( "Password2" );
12: }
13: }
4. The DataBinding engine will convert the exception to a visual state change notification to your control, which must already support the Visual State Group: ValidationStates which consists of
? Valid
? InvalidUnfocused
? InvalidFocused
When the visual state changes, the Visual State Manager calls the appropriate storyboard as usual. The effect is that the UI is updated to inform the user of the validation problem,
The following controls are provided with default control templates (which of course you can modify) to support Data Validation
? Textbox
? PasswordBox
? CheckBox
? RadioButton
? ListBox
? ComboBox
The Silverlight Learn page has a video on Data Validation and another specifically on Password Validation, with more to come, and there are in-depth mini-tutorials on data validation here and especially here
[ Working Demo Follows ]
Previous: Based-on Styles More: Easing Is Easier
[We are transitioning the DocumentWiki into linked blog pages for better access and visibility]