轉帖|其它|編輯:郝浩|2010-12-23 14:08:12.000|閱讀 565 次
概述:binding是一個非常重要的特性,所有的驗證顯示也都是通過它來實現的。在Silverlight4中由IDataErrorInfo and INotifyDataErrorInfo interfaces 來實現。
# 界面/圖表報表/文檔/IDE等千款熱門軟控件火熱銷售中 >>
binding是一個非常重要的特性,所有的驗證顯示也都是通過它來實現的。在Silverlight4中由IDataErrorInfo and INotifyDataErrorInfo interfaces 來實現。
1 捕獲異常
<TextBox Grid.Row="0" Grid.Column="1"
Text="{Binding LastName, Mode=TwoWay,
ValidatesOnExceptions=True}" />
public string LastName
{
get { return _lastName; }
set
{
if (string.IsNullOrEmpty(value))
throw new Exception("姓不能為空");
_lastName = value;
NotifyPropertyChanged("LastName");
}
2、使用INotifyPropertyChanged, IDataErrorInfo 驗證和顯示
public class Employee : INotifyPropertyChanged, IDataErrorInfo {
private string _lastName;
public string LastName {
get { return _lastName; }
set {
if (string.IsNullOrEmpty(value))
_dataErrors["LastName"] = "Last Name is required";
else if (value.Trim().Length < 2)
_dataErrors["LastName"] =
"Last Name must be at least 2 letters long.";
else
if (_dataErrors.ContainsKey("LastName"))
_dataErrors.Remove("LastName");
_lastName = value;
NotifyPropertyChanged("LastName");
}
}
#region IDataErrorInfo Members
private string _dataError = string.Empty;
public string Error {
get { return _dataError; }
}
private Dictionary<string, string> _dataErrors =
new Dictionary<string, string>();
public string this[string columnName] {
get {
if (_dataErrors.ContainsKey(columnName))
return _dataErrors[columnName];
else
return null;
}
}
}
xaml 中
<TextBox Grid.Row="0"
Grid.Column="1"
Text="{Binding LastName, Mode=TwoWay, ValidatesOnDataErrors=True}" />
3、異常驗證INotifyDataErrorInfo
IEnumerable INotifyDataErrorInfo.GetErrors(string propertyName)
{
if (!string.IsNullOrEmpty(propertyName))
{
if (_validationErrors.ContainsKey(propertyName))
return _validationErrors[propertyName];
else
return null;
}
else
{
return _classValidationErrors;
}
}
bool INotifyDataErrorInfo.HasErrors
{
get
{
if (_classValidationErrors.Count > 0)
return true;
foreach (string key in _validationErrors.Keys)
{
if (_validationErrors[key].Count > 0)
return true;
}
return false;
}
}
#endregion
4、為你的實體類添加屬性
[Required]
[StringLength(320)]
[RegularExpression(@"^[a-zA-Z][\w\.&-]*[a-zA-Z0-9]@[a-zA-Z0-9]
[\w\.-]*[a-zA-Z0-9]\.[a-zA-Z\.]*[a-zA-Z]$")]
public string EmailAddress { get; set; }
[Range(0, 20)]
public int NumberOfChildren { get; set; }
還可以自定義
方法1
public class CustomValidationMethods
{
public static ValidationResult NameBeginsWithB(string name)
{
if (name.StartsWith("B"))
return ValidationResult.Success;
else
return new ValidationResult("Name does not begin with 'B'");
}
}
[CustomValidation(typeof(CustomValidationMethods),
"NameBeginsWithB"]
public string LastName { get; set; }
方法2
public class NameBeginsWithBAttribute : ValidationAttribute
{
protected override ValidationResult IsValid(
object value, ValidationContext validationContext)
{
if (!(value is string))
return new ValidationResult(
"Incorrect data type. Expected string");
if (string.IsNullOrEmpty((string)value))
return ValidationResult.Success;
if (((string)value).StartsWith("B"))
return ValidationResult.Success;
else
return new ValidationResult(
string.Format("{0} does not begin with 'B'",
validationContext.DisplayName));
}
}
本站文章除注明轉載外,均為本站原創或翻譯。歡迎任何形式的轉載,但請務必注明出處、不得修改原文相關鏈接,如果存在內容上的異議請郵件反饋至chenjj@fc6vip.cn
文章轉載自:博客園