WPF使用IDataErrorInfo接口进行数据校验 - 简书

2021-06-08 23:03

阅读:609

标签:aaa   only   val   使用   shu   override   protect   reac   ali   

原文:WPF使用IDataErrorInfo接口进行数据校验 - 简书

    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 Dictionarystring
, string> _errorMap = new Dictionarystring, string>(); protected override bool SetProperty(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接口进行数据校验 - 简书

标签:aaa   only   val   使用   shu   override   protect   reac   ali   

原文地址:https://www.cnblogs.com/lonelyxmas/p/10682644.html


评论


亲,登录后才可以留言!