#region 數(shù)據(jù)校驗(yàn)
#region IDataErrorInfo 成員
private string _dataError = string.Empty;
private Dictionary<string, string> _dataErrors = new Dictionary<string, string>();
public string Error
{
get { return _dataError; }
}
public string this[string columnName]
{
get
{
if (_dataErrors.ContainsKey(columnName))
return _dataErrors[columnName];
else
return null;
}
}
#endregion
public void AddError(string name, string error)
{
_dataErrors[name] = error;
RaisePropertyChanged(name);
}
public void RemoveError(string name)
{
if (_dataErrors.ContainsKey(name))
{
_dataErrors.Remove(name);
RaisePropertyChanged(name);
}
}
public void ClearError()
{
var keys = new string[_dataErrors.Count];
_dataErrors.Keys.CopyTo(keys, 0);
foreach (var key in keys)
{
this.RemoveError(key);
}
}
public bool Validate()
{
this.ClearError();
var results = new List<ValidationResult>();
if (!Validator.TryValidateObject(this, new ValidationContext(this, null, null), results, true))
{
foreach (var result in results)
{
this.AddError(result.MemberNames.First(), result.ErrorMessage);
}
return false;
}
return true;
}
public void Validate(string name)
{
var results = new List<ValidationResult>();
if (Validator.TryValidateProperty(name, new ValidationContext(this, null, null), results))
{
RemoveError(name);
}
}
#endregion