With silverlight 3 we were able to validate data using ‘INotifyPropertyChanged’ (System.ComponenetModel.INotifyPropertyChanged) interface.
Now in silverlight 4 we have one new interface called ‘INotifyDataErrorInfo’ (System.ComponenetModel.INotifyDataErrorInfo) as well.
Any Entity class can implement both or any of these interfaces to validate the data.
I am using the Entity class “User” to validate the data (Name and Email) as shown in the image below……
Here “Name” is validating through “INotifyDataErrorInfo” and “Email” is validating through “INotifyPropertyChanged”.
Validation using “INotifyPropertyChanged” :
To use “INotifyPropertyChanged”, we need to implement one event “PropertyChanged” in code behind file as shown in the image above (second image in the post).
We also need to set couple of attributes (“ValidatesOnExceptions”, “NotifyOnValidationError”) on the control in XAML code which actually performs the validation as shown in the image below……
Now when the value of property “email” is changed, code in the following image throws an exception…..
And this exception is then handled by silverlight validation framework and it creates the error visualization on silverlight control as shown in the folowing image…….
Validation using “INotifyDataErrorInfo” :
To use “INotifyDataErrorInfo” we need to implement one event “ErrorsChanged”, one method “GetErrors” and one property “HasErrors” in the code behind file as shown in the image above (second image in the current blog post).
Here we do not need to implement anything in XAML code to perform validation as in case of “INotifyPropertyChanged”.
Now when the value of property “name” is changed, code in the following image validates the value of “name” and called the event “ErrorsChanged” and which in turns called the method “GetErrors()” which returns the “errors” (or null if there is no error) as shown in the image below…….
And then if “GetError()” method returns any error is then handled by silverlight validation framework and it creates the error visualization on silverlight control as shown in the folowing image…….
Silverlight validation framework seems lot more sound with silverlight 4 now in compare with silverlight 3. Silverlight 4 validation framework also supports “Asynchronous Data Validation”, which I will discuss in my next silverlight blog post.
For more info on silverlight, just go to, http://www.silverlight.net
For more on silverlight 4, click here, http://www.silverlight.net/getstarted/silverlight-4/
For more details on Silverlight 4.0, you can also browse through following blogs………
http://timheuer.com/blog/ — Tim Heuer Blog – Silverlight Program Manager
http://blogs.silverlight.net/blogs/jesseliberty/ — Jesse Liberty Blog – Silverlight Program Manager
http://johnpapa.net/ — John Papa website
http://adamkinney.wordpress.com/ — Adam Kinney Blog — For Silverlight graphics, interface and Expression Blend