This is a mirror of official site: http://jasper-net.blogspot.com/

Validating Data in Silverlight 4 Applications – IDataErrorInfo

| Sunday, August 22, 2010
Data validation is an important part of any application. If you've written code to validate data in Silverlight applications prior to the release of Silverlight 4, you've probably discovered that there isn't a great way to ensure that data is entered correctly and errors displayed properly by controls. The typical technique used for validating data was to throw exceptions within property setter blocks as data was found to be invalid. Controls bound to a property could be notified of a data validation exception by setting ValidatesOnExceptions to true in the data binding. Although this technique certainly works, there are some new options available in Silverlight 4 that can be used without resorting to throwing exceptions.

With the release of Silverlight 4 two new interfaces are now available including IDataErrorInfo and INotifyDataErrorInfo. In this post I'll walk through the process of using the IDataErrorInfo interface which provides a more simplistic way to validate data and provide end users with notifications. Let's start by taking a look at the interface's members:

  1. public
 interface IDataErrorInfo  
  • {  
  •    string this[string columnName] { get; }  
  •    string Error { get; }  
  • }  

  • The IDataErrorInfo interface only contains two members including Error which can return error information about the overall object and a default indexer property that can be used to write validation logic for different properties within an object. You'll implement the IDataErrorInfo interface on a client-side entity class in your Silverlight project such as the Person class shown next:

    1. public
     class Person : INotifyPropertyChanged, IDataErrorInfo  
  • {  
  •    public string this[string propertyName]  
  •    {  
  •        get  
  •        {  
  •           ….  
  •        }  
  •    }  
  •   
  •    public string Error  
  •    {  
  •        get { …. }  
  •    }  
  • }  
  •  

    Read more: Dan Wahlin's WebLog

    Posted via email from .NET Info

    0 comments: