Steady Silverlight (51)-4.0 binding data validation Idataerrorinfo,inotifydataerrorinfo
Introduced
Silverlight 4.0 Data validation:
* IDataErrorInfo-Provides custom validation support for data entity classes. The. NET Framework also has this interface to facilitate porting
* Inotifydataerrorinfo-Provides custom validation support for data entity classes, which is more powerful than the IDataErrorInfo feature. Inotifydataerrorinfo supports asynchronous authentication, which means that it can invoke Web services through authentication methods and update the error collection with callback methods to add server-side validation
Online Demo
Http://www.cnblogs.com/webabcd/archive/2010/08/09/1795417.html
Example
1. Demo IDataErrorInfo Application
IDataErrorInfoModel.cs
Code
/*
* IDataErrorInfo-Provides custom validation support for data entity classes. The. NET Framework also has this interface to facilitate porting
* String error-Gets the validation error message for the object
* String this[string ColumnName]-Gets the validation error message for the specified field of the object
*/
Using System;
Using System.Net;
Using System.Windows;
Using System.Windows.Controls;
Using System.Windows.Documents;
Using System.Windows.Ink;
Using System.Windows.Input;
Using System.Windows.Media;
Using System.Windows.Media.Animation;
Using System.Windows.Shapes;
Using System.Collections.Generic;
Using System.ComponentModel.DataAnnotations;
Namespace Silverlight40.binding
{
public class IDataErrorInfoModel:System.ComponentModel.IDataErrorInfo
{
Notification of validation errors
Private Const string Id_error = "ID cannot be less than 10";
Private Const string Name_error = "NAME cannot contain spaces";
Private Const string name_warning = "NAME cannot be less than 5 characters";
Used to save validation error messages. Key holds the validated field name; value saves a list of validation error messages for the corresponding field
Private dictionary<string, list<string>> errors = new dictionary<string, list<string>> ();
private int _id;
[Display (Name = "identity")]
public int Id
{
get {return _id;}
Set
{
if (Value > 1000)
throw new Exception ("too big");
if (isidvalid (value) && _id!= value)
_id = value;
}
}
private string _name;
[Display (name = "Names")]
public string Name
{
get {return _name;}
Set
{
if (isnamevalid (value) && _name!= value)
_name = value;
}
}
public bool Isidvalid (int value)
{
bool IsValid = true;
if (Value < 10)
{
Adderror ("Id", id_error);
IsValid = false;
}
Else
{
Removeerror ("Id", id_error);
}
return isValid;
}
public bool Isnamevalid (string value)
{
bool IsValid = true;
if (value. Contains (""))
{
Adderror ("Name", name_error);
IsValid = false;
}
Else
{
Removeerror ("Name", name_error);
}
if (value. Length < 5)
{
Adderror ("Name", name_warning);
IsValid = false;
}
Else
{
Removeerror ("Name", name_warning);
}
return isValid;
}
public void Adderror (String propertyname, string error)
{
if (!errors. ContainsKey (PropertyName))
Errors[propertyname] = new list<string> ();
if (!errors[propertyname). Contains (Error))
Errors[propertyname]. ADD (Error);
}
public void Removeerror (String propertyname, string error)
{
if (errors. ContainsKey (PropertyName) && Errors[propertyname]. Contains (Error))
{
Errors[propertyname]. Remove (Error);
if (errors[propertyname). Count = 0)
Errors. Remove (PropertyName);
}
}
public string Error
{
get {return errors. Count > 0? "There is a validation error": "No validation Errors"; }
}
public string This[string PropertyName]
{
Get
{
if (errors. ContainsKey (PropertyName))
return string. Join (Environment.NewLine, Errors[propertyname]);
Else
return null;
}
}
}
}