using System; using System.Collections; using System.Collections.Generic; using System.ComponentModel.DataAnnotations; using System.ComponentModel; using System.Linq; namespace LogoForceTestApp.Modules.MainModule { public class ValidationTemplate : IDataErrorInfo, INotifyDataErrorInfo { readonly INotifyPropertyChanged _target; readonly ValidationContext _validationContext; readonly List _validationResults; public ValidationTemplate(INotifyPropertyChanged target) { this._target = target; _validationContext = new ValidationContext(target, null, null); _validationResults = new List(); Validator.TryValidateObject(target, _validationContext, _validationResults, true); target.PropertyChanged += Validate; } void Validate(object sender, PropertyChangedEventArgs e) { _validationResults.Clear(); Validator.TryValidateObject(_target, _validationContext, _validationResults, true); var hashSet = new HashSet(_validationResults.SelectMany(x => x.MemberNames)); foreach (var error in hashSet) { RaiseErrorsChanged(error); } } public IEnumerable GetErrors(string propertyName) { return _validationResults.Where(x => x.MemberNames.Contains(propertyName)) .Select(x => x.ErrorMessage); } public bool HasErrors { get { return _validationResults.Count > 0; } } public string Error { get { var strings = _validationResults.Select(x => x.ErrorMessage) .ToArray(); return string.Join(Environment.NewLine, strings); } } public string this[string propertyName] { get { var strings = _validationResults.Where(x => x.MemberNames.Contains(propertyName)) .Select(x => x.ErrorMessage) .ToArray(); return string.Join(Environment.NewLine, strings); } } public event EventHandler ErrorsChanged; void RaiseErrorsChanged(string propertyName) { ErrorsChanged?.Invoke(this, new DataErrorsChangedEventArgs(propertyName)); } } }