private void LoadMetadata(bool forceUpdate)
        {
            ValidationMetadata objB              = null;
            object             entity            = null;
            BindingExpression  bindingExpression = null;

            if (!string.IsNullOrEmpty(this.PropertyPath))
            {
                entity = base.DataContext;
                objB   = ValidationHelper.ParseMetadata(this.PropertyPath, entity);
            }
            else if (this.Target != null)
            {
                objB = ValidationHelper.ParseMetadata(this.Target, forceUpdate, out entity, out bindingExpression);
            }
            if (!ReferenceEquals(this.ValidationMetadata, objB))
            {
                this.ValidationMetadata = objB;
                if (!this._descriptionOverridden)
                {
                    string description = null;
                    if (this.ValidationMetadata != null)
                    {
                        description = this.ValidationMetadata.Description;
                    }
                    base.SetValue(DescriptionProperty, description);
                }
            }
        }
 internal static ValidationMetadata ParseMetadata(string bindingPath, object entity)
 {
     if ((entity != null) && !string.IsNullOrEmpty(bindingPath))
     {
         PropertyInfo property = GetProperty(GetCustomOrCLRType(entity), bindingPath);
         if (property != null)
         {
             ValidationMetadata metadata = new ValidationMetadata();
             foreach (object obj2 in property.GetCustomAttributes(false))
             {
                 RequiredAttribute attribute = obj2 as RequiredAttribute;
                 if (attribute != null)
                 {
                     metadata.IsRequired = true;
                 }
                 else
                 {
                     DisplayAttribute attribute2 = obj2 as DisplayAttribute;
                     if (attribute2 != null)
                     {
                         metadata.Description = attribute2.GetDescription();
                         metadata.Caption     = attribute2.GetName();
                     }
                 }
             }
             if (metadata.Caption == null)
             {
                 metadata.Caption = property.Name;
             }
             return(metadata);
         }
     }
     return(null);
 }
 internal static void SetValidationMetadata(DependencyObject inputControl, ValidationMetadata value)
 {
     if (inputControl == null)
     {
         throw new ArgumentNullException("inputControl");
     }
     inputControl.SetValue(ValidationMetadataProperty, value);
 }
        internal static ValidationMetadata ParseMetadata(FrameworkElement element, bool forceUpdate, out object entity, out BindingExpression bindingExpression)
        {
            entity            = null;
            bindingExpression = null;
            if (element == null)
            {
                return(null);
            }
            if (!forceUpdate)
            {
                ValidationMetadata metadata = element.GetValue(ValidationMetadataProperty) as ValidationMetadata;
                if (metadata != null)
                {
                    return(metadata);
                }
            }
            BindingExpression expression = null;

            foreach (FieldInfo info in element.GetType().GetFields(BindingFlags.FlattenHierarchy | BindingFlags.Public | BindingFlags.Static))
            {
                if (ReferenceEquals(info.FieldType, typeof(DependencyProperty)))
                {
                    expression = element.GetBindingExpression((DependencyProperty)info.GetValue(null));
                    if (((expression != null) && (expression.ParentBinding != null)) && (expression.ParentBinding.Path != null))
                    {
                        entity = (expression.DataItem != null) ? expression.DataItem : element.DataContext;
                        if (entity != null)
                        {
                            if (expression.ParentBinding.Mode == BindingMode.TwoWay)
                            {
                                bindingExpression = expression;
                                break;
                            }
                            if ((bindingExpression == null) || (string.Compare(expression.ParentBinding.Path.Path, bindingExpression.ParentBinding.Path.Path, StringComparison.Ordinal) < 0))
                            {
                                bindingExpression = expression;
                            }
                        }
                    }
                }
            }
            if (bindingExpression == null)
            {
                return(null);
            }
            ValidationMetadata metadata2 = ParseMetadata(bindingExpression.ParentBinding.Path.Path, entity);

            element.SetValue(ValidationMetadataProperty, metadata2);
            return(metadata2);
        }
示例#5
0
        private void LoadMetadata(bool forceUpdate)
        {
            ValidationMetadata objB              = null;
            object             entity            = null;
            BindingExpression  bindingExpression = null;

            if (!string.IsNullOrEmpty(this.PropertyPath))
            {
                entity = base.DataContext;
                objB   = ValidationHelper.ParseMetadata(this.PropertyPath, entity);
            }
            else if (this.Target != null)
            {
                objB = ValidationHelper.ParseMetadata(this.Target, forceUpdate, out entity, out bindingExpression);
            }
            if (!ReferenceEquals(this.ValidationMetadata, objB))
            {
                this.ValidationMetadata = objB;
                if (this.ValidationMetadata == null)
                {
                    if (this._canContentUseMetaData)
                    {
                        this.SetContentInternally(null);
                    }
                }
                else
                {
                    string caption = this.ValidationMetadata.Caption;
                    if ((caption != null) && this._canContentUseMetaData)
                    {
                        this.SetContentInternally(caption);
                    }
                }
                if (!this._isRequiredOverridden)
                {
                    bool flag = (this.ValidationMetadata != null) && this.ValidationMetadata.IsRequired;
                    base.SetValue(IsRequiredProperty, flag);
                }
            }
        }