What’s New In Silverlight 3 – Validation

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

 data validation.Valid1

    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

data validation.Valid2

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

data validation.Valid3

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,

data validation.Validation2

 

 

 

 

 

 

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]

About Jesse Liberty

Jesse Liberty has three decades of experience writing and delivering software projects and is the author of 2 dozen books and a couple dozen online courses. His latest book, Building APIs with .NET will be released early in 2025. Liberty is a Senior SW Engineer for CNH and he was a Senior Technical Evangelist for Microsoft, a Distinguished Software Engineer for AT&T, a VP for Information Services for Citibank and a Software Architect for PBS. He is a Microsoft MVP.
This entry was posted in z Silverlight Archives. Bookmark the permalink.