class ValidationBindableBase : BindableBase, IDataErrorInfo
{
public string this[string columnName]
{
get
{
if (_errorMap.ContainsKey(columnName))
{
var error = _errorMap[columnName];
_errorMap.Remove(columnName);
return error;
}
return null;
}
}
public string Error => string.Join("\n",_errorMap.Values);
private readonly Dictionary<string, string> _errorMap = new Dictionary<string, string>();
protected override bool SetProperty<T>(ref T storage, T value, [CallerMemberName]string propertyName = null)
{
var result = base.SetProperty(ref storage, value, propertyName);
var type = this.GetType();
foreach (var methodInfo in type.GetMethods())
{
if (methodInfo.Name == propertyName + "Validation"&& methodInfo.ReturnType == typeof(string) && methodInfo.GetParameters().Length == 0)
{
_errorMap.Add(propertyName,(string)methodInfo.Invoke(this, null));
}
}
return result;
}
}
WPF使用IDataErrorInfo接口进行数据校验
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。
推荐阅读更多精彩内容
- 本文部分内容来自互联网。 在进行REST接口开发的时候,需要对POST/PUT等接口传入的参数进行一些校验的时候,...
- 三、使用zlib库对收到数据进行解压 使用zlib库:http://www.zlib.net/此软件编译使用的是z...