What’s New In Silverlight 3 - Validation - Jesse Liberty - Silverlight Geek Page view counter

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 ]

PreviousBased-on Styles        More: Easing Is Easier    

[We are transitioning the DocumentWiki into linked blog pages for better access and visibility]

Published Saturday, July 18, 2009 2:05 PM by jesseliberty
Filed under:

Comments

# What???s New In SL3 ??? Based-on Styles - Jesse Liberty - Silverlight Geek

Pingback from  What???s New In SL3 ??? Based-on Styles - Jesse Liberty - Silverlight Geek

# What’s New In SL3 – Based-on Styles

In Silverlight 3 it is now possible to build a new style based on an existing style, thus reducing duplication

Saturday, July 18, 2009 2:17 PM by Microsoft Weblogs

# What’s New In Silverlight 3 - Validation - Jesse Liberty - Silverlight Geek

Thank you for submitting this cool story - Trackback from DotNetShoutout

Saturday, July 18, 2009 3:40 PM by DotNetShoutout

# New and Notable 344

Silverlight 3 Business Apps Example for Silverlight 3 RTM and .NET RIA Services July Update: Part 8: WCF Based Data Source Building Silverlight 2 and Silverlight 3 from Visual Studio 2008 New in Silverlight 3: Perspective Transforms What's New in SL3

# What’s New In Silverlight 3 - Validation - Jesse Liberty - Silverlight Geek

Thank you for submitting this cool story - Trackback from NewsPeeps

Saturday, July 18, 2009 8:34 PM by NewsPeeps

# What???s New In Silverlight 3 - Validation | Silverlight Travel

Pingback from  What???s New In Silverlight 3 - Validation | Silverlight Travel

# WNISL3-TOC

What's New In Silverlight 3 - Table of Contents 3d Perspective Transform Based on Styles Capture Video

Monday, July 20, 2009 4:00 PM by Jesse Liberty - Silverlight Geek

# What’s New In Silverlight 3? Easier Easing

Animated movement often seems much more natural and realistic if the moving object accelerates up to

Monday, July 20, 2009 4:09 PM by Microsoft Weblogs

# re: What’s New In Silverlight 3 - Validation

Hi,

I have a textbox control which is bound to int value so I need validation on user's only integer input.

I tried to throw validation exception in my custom converter i my binding, but this exception was not captured in UI. So what's the problem with this scenario?

Tomas

Tuesday, July 21, 2009 8:23 AM by tomas.k

# What???s New In Silverlight 3? Easier Easing - Jesse Liberty - Silverlight Geek

Pingback from  What???s New In Silverlight 3? Easier Easing - Jesse Liberty - Silverlight Geek

# re: What’s New In Silverlight 3 - Validation

I have a datagrid which is bound to a observablecollection. It consist of a column StartDate and one which is EndDate, these two columns consist of a Datepicker.

Now the problem is how can i implement a validation which restricts user to put enddate not before startdate and viceversa.

Thursday, July 30, 2009 7:52 AM by madhuranjan

# ??????????????? 3??? ????????? ??????(??????) « jin_u as blog

Pingback from  ??????????????? 3??? ????????? ??????(??????) « jin_u as blog

# Programming with Silverlight, WPF & .NET » Was ist neu in SL 3?

Pingback from  Programming with Silverlight, WPF & .NET » Was ist neu in SL 3?

# re: What’s New In Silverlight 3 - Validation

>Test the data for validity and throw an exception for invalid data in any of the following three places:

   ⁃    Binding Type Converter

Unfortunately "Binding Type Converter" does not work. See silverlight.net/.../272082.aspx

Wednesday, August 19, 2009 10:45 AM by ajdotnet

# re: What’s New In Silverlight 3 - Validation

This doesn't work when following your video. Something is missing. The UI does not handle the ArgumentException and VS just throws the exception. I've seen several others stating the same problem.

Saturday, September 05, 2009 5:02 PM by ekleeman

# re: What’s New In Silverlight 3 - Validation

I can't seem to figure out how to use business object property validation with WCF? Any help would be appreciated.

Thursday, November 12, 2009 6:27 PM by lentyai

# Silverlight: how to fix the Silverlight Toolkit templates to add Validation States at Primordial Code

Pingback from  Silverlight: how to fix the Silverlight Toolkit templates to add Validation States at Primordial Code

# Silverlight 4 with Tim Heuer and Jesse Liberty

While the attendees of PDC09 likely expected to hear a plethora of announcements regarding Silverlight

Monday, December 14, 2009 6:34 PM by Craig Shoemaker