首页 > Windows开发 > 详细

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

时间:2019-04-10 13:05:09      阅读:207      评论:0      收藏:0      [点我收藏+]
原文: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 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接口进行数据校验 - 简书

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

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!