Beispiel #1
0
        string IDataErrorInfo.this[string propertyName]
        {
            get
            {
                string error        = string.Empty;
                var    dpDescriptor = DependencyPropertyDescriptor.FromName(propertyName, this.GetType(), this.GetType());

                if (dpDescriptor != null)
                {
                    var results = new List <ValidationResult>(1);

                    var result = Validator.TryValidateProperty(
                        value: this.GetValue(dpDescriptor.DependencyProperty),
                        validationContext: new ValidationContext(this)
                    {
                        MemberName = propertyName
                    },
                        validationResults: results);

                    if (!result)
                    {
                        var validationResult = results.First();
                        error = validationResult.ErrorMessage;

                        if (ValidationErrors.Contains(propertyName))
                        {
                            ValidationErrors[propertyName] = error;
                        }

                        else
                        {
                            ValidationErrors.Add(propertyName, error);
                        }
                    }

                    else
                    {
                        if (ValidationErrors.Contains(propertyName))
                        {
                            ValidationErrors.Remove(propertyName);
                        }
                    }
                }

                IsValid = ValidationErrors.Count > 0 ? false : true;
                return(error);
            }
        }