using System; using System.Collections.Generic; using System.ComponentModel; using System.ComponentModel.DataAnnotations; using System.Linq; using System.Runtime.CompilerServices; using System.Text; using System.Threading.Tasks; namespace MaterialTraceabilityUI.Common { public class NotifyPropertyBase : INotifyPropertyChanged, IDataErrorInfo { private Dictionary dataErrors = new Dictionary(); public string this[string columnName] { get { ValidationContext vc = new ValidationContext(this); vc.MemberName = columnName; var res = new List(); var result = Validator.TryValidateProperty(this.GetType().GetProperty(columnName).GetValue(this), vc, res); if (res.Count > 0) { AddDic(dataErrors, vc.MemberName); return string.Join(Environment.NewLine, res.Select(r => r.ErrorMessage).ToArray()); } RemoveDic(dataErrors, vc.MemberName); return null; } } #region 附属方法 /// /// 移除字典 /// /// /// private void RemoveDic(Dictionary dics, String dicKey) { dics.Remove(dicKey); } /// /// 添加字典 /// /// /// private void AddDic(Dictionary dics, String dicKey) { if (!dics.ContainsKey(dicKey)) dics.Add(dicKey, ""); } #endregion public string Error => null; public bool IsValided { get { return this.dataErrors == null || this.dataErrors.Count == 0; } } public event PropertyChangedEventHandler PropertyChanged; public void Notify([CallerMemberName] string propName = "") { if (PropertyChanged != null) this.PropertyChanged(this, new PropertyChangedEventArgs(propName)); } } }