|
| 1 | +using System; |
| 2 | +using System.Collections; |
| 3 | +using System.Collections.Generic; |
| 4 | +using System.ComponentModel; |
| 5 | +using System.Linq; |
| 6 | +using System.Text; |
| 7 | +using System.Threading.Tasks; |
| 8 | + |
| 9 | +namespace MahApps.Demo.Core |
| 10 | +{ |
| 11 | + public class BaseClass : INotifyPropertyChanged, INotifyDataErrorInfo |
| 12 | + { |
| 13 | + #region INotifyPropertyChanged |
| 14 | + |
| 15 | + // This event tells the UI to update |
| 16 | + public event PropertyChangedEventHandler PropertyChanged; |
| 17 | + |
| 18 | + public void RaisePropertyChanged(string PropertyName) |
| 19 | + { |
| 20 | + PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(PropertyName)); |
| 21 | + } |
| 22 | + |
| 23 | + #endregion INotifyPropertyChanged |
| 24 | + |
| 25 | + #region INotifyDataErrorInfo |
| 26 | + |
| 27 | + public event EventHandler<DataErrorsChangedEventArgs> ErrorsChanged; |
| 28 | + |
| 29 | + public bool HasErrors => _errorsByPropertyName.Count > 0; |
| 30 | + |
| 31 | + private readonly Dictionary<string, List<string>> _errorsByPropertyName = new Dictionary<string, List<string>>(); |
| 32 | + |
| 33 | + public IEnumerable GetErrors(string propertyName) |
| 34 | + { |
| 35 | + if (string.IsNullOrEmpty(propertyName)) |
| 36 | + { |
| 37 | + return null; |
| 38 | + } |
| 39 | + else |
| 40 | + { |
| 41 | + return _errorsByPropertyName.ContainsKey(propertyName) |
| 42 | + ? _errorsByPropertyName[propertyName] |
| 43 | + : null; |
| 44 | + } |
| 45 | + } |
| 46 | + |
| 47 | + public bool GetHasError(string PropertyName) |
| 48 | + { |
| 49 | + return _errorsByPropertyName.ContainsKey(PropertyName); |
| 50 | + } |
| 51 | + |
| 52 | + public void AddError(string propertyName, string error) |
| 53 | + { |
| 54 | + if (!_errorsByPropertyName.ContainsKey(propertyName)) |
| 55 | + _errorsByPropertyName[propertyName] = new List<string>(); |
| 56 | + |
| 57 | + if (!_errorsByPropertyName[propertyName].Contains(error)) |
| 58 | + { |
| 59 | + _errorsByPropertyName[propertyName].Add(error); |
| 60 | + OnErrorsChanged(propertyName); |
| 61 | + } |
| 62 | + } |
| 63 | + |
| 64 | + public void ClearErrors(string propertyName) |
| 65 | + { |
| 66 | + if (_errorsByPropertyName.ContainsKey(propertyName)) |
| 67 | + { |
| 68 | + _errorsByPropertyName.Remove(propertyName); |
| 69 | + OnErrorsChanged(propertyName); |
| 70 | + } |
| 71 | + } |
| 72 | + |
| 73 | + public void OnErrorsChanged(string propertyName) |
| 74 | + { |
| 75 | + ErrorsChanged?.Invoke(this, new DataErrorsChangedEventArgs(propertyName)); |
| 76 | + } |
| 77 | + |
| 78 | + #endregion INotifyDataErrorInfo |
| 79 | + } |
| 80 | +} |
0 commit comments