private void Source_ErrorsChanged(object sender, DataErrorsChangedEventArgs e) { if (this.validableProperties.ContainsKey(e.PropertyName)) { var context = this.validableProperties[e.PropertyName]; var source = context.GetTarget(); if (source == null) { this.validableProperties.Remove(e.PropertyName); return; } var errors = source.GetErrors(e.PropertyName) as IEnumerable <string>; this.allErrors.Remove(e.PropertyName); if (errors != null) { this.allErrors.Add(e.PropertyName, errors.Join("\r\n")); } ValidationProperties.SetHasErrors(this.AssociatedObject, this.allErrors.Count > 0); ValidationProperties.SetErrors(this.AssociatedObject, this.allErrors.Count == 0 ? string.Empty : this.allErrors.Values.Join("\r\n\r\n")); } }
private void Source_Validating(object sender, ValidationEventArgs e) { if (!this.validableProperties.ContainsKey(e.PropertyName)) { return; } var context = this.validableProperties[e.PropertyName]; var source = context.GetTarget(); if (source == null) { this.validableProperties.Remove(e.PropertyName); return; } var property = source.GetType().GetPropertyEx(e.PropertyName); if (property == null) { return; } var attrib = property.GetCustomAttribute <IsMandatoryAttribute>(); if (attrib == null || !attrib.IsDeactivatable) { return; } ValidationProperties.SetIsMandatory(this.AssociatedObject, attrib.IsEnabled(source, property)); }
private void SetValidationInfo(BindingExpression bindingExpression) { if (bindingExpression == null) { return; } // get the binding sources in order to do some voodoo on them var source = bindingExpression.ParentBinding.Source as INotifyDataErrorInfo; if (source == null) { source = this.AssociatedObject.DataContext as INotifyDataErrorInfo; } // we don't support too complicated binding paths var propertyName = bindingExpression.ParentBinding.Path.Path; // check if the property name end with a ']' ... We dont support that if (propertyName.Right(1) == "]") { return; } if (propertyName.Contains('.')) { source = XAMLHelper.GetSourceFromPath(source, propertyName) as INotifyDataErrorInfo; propertyName = propertyName.Right(propertyName.Length - propertyName.LastIndexOf('.') - 1); } // if our source is still null then either the source does not implements the // INotifyDataErrorInfo interface or we dont have a valid source at all In both cases we // just give up if (source == null) { return; } // then let us check if the binding source have validation attributes on them var property = source.GetType().GetPropertyEx(propertyName); if (property == null) { return; } if (!this.validableProperties.ContainsKey(propertyName) && property.GetCustomAttributes().Any(x => x is ValidatorAttributeBase)) { this.validableProperties.Add(propertyName, new WeakReference <INotifyDataErrorInfo>(source)); source.ErrorsChanged += this.Source_ErrorsChanged; (source as IValidatableViewModel).IsNotNull(x => x.Validating += Source_Validating); // Also... If we have a mandatory attribute... Add it if (property.GetCustomAttribute <IsMandatoryAttribute>() != null) { ValidationProperties.SetIsMandatory(this.AssociatedObject, true); } } }