Esempio n. 1
0
        public static void SetValidationMetadata(DependencyObject inputControl, ValidationMetadata value)
        {
            if (inputControl == null)
            {
                throw new ArgumentNullException(nameof(inputControl));
            }

            inputControl.SetValue(ValidationMetadataProperty, value);
        }
Esempio n. 2
0
        public static ValidationMetadata ParseMetadata(string bindingPath, object entity)
        {
            if (entity == null || string.IsNullOrEmpty(bindingPath))
            {
                return(null);
            }

            var property = GetProperty(GetCustomOrCLRType(entity), bindingPath);

            if (property == null)
            {
                return(null);
            }

            var validationMetadata = new ValidationMetadata();

            foreach (var customAttribute in property.GetCustomAttributes(false))
            {
                if (customAttribute is RequiredAttribute)
                {
                    validationMetadata.IsRequired = true;
                }
                else
                {
                    var displayAttribute = customAttribute as DisplayAttribute;
                    if (displayAttribute == null)
                    {
                        continue;
                    }

                    validationMetadata.Description = displayAttribute.GetDescription();
                    validationMetadata.Caption     = displayAttribute.GetName();
                }
            }

            if (validationMetadata.Caption == null)
            {
                validationMetadata.Caption = property.Name;
            }

            return(validationMetadata);
        }