Пример #1
0
        public static string ValidateProperty(this ValidationViewModelBase dp, string propertyName)
        {
            if (string.IsNullOrEmpty(propertyName))
            {
                return(string.Empty);
            }
            var targetType    = dp.GetType();
            var propertyValue = targetType.GetProperty(propertyName).GetValue(dp, null);

            return(dp.ValidateProperty(propertyValue, propertyName));
        }
Пример #2
0
        public static string ValidateProperty(this ValidationViewModelBase dp, object value, string propertyName)
        {
            ValidationContext vc = new ValidationContext(dp)
            {
                MemberName = propertyName
            };
            List <System.ComponentModel.DataAnnotations.ValidationResult> vr = new List <System.ComponentModel.DataAnnotations.ValidationResult>();

            if (Validator.TryValidateProperty(value, vc, vr))
            {
                dp.RemoveError(propertyName);
                return(string.Empty);
            }
            else
            {
                dp.AddError(propertyName, vr.FirstOrDefault().ErrorMessage);
                return(vr.FirstOrDefault().ErrorMessage);
            }
        }
Пример #3
0
        public static bool ValidateViewModel(this ValidationViewModelBase dp)
        {
            string strValid = string.Empty;
            var    props    = dp.GetType()
                              .GetProperties()
                              .Where(p => ((ValidationAttribute[])p.GetCustomAttributes(typeof(ValidationAttribute), true)).Length != 0);

            foreach (PropertyInfo item in props)
            {
                var value = item.GetValue(dp, null);
                strValid = dp.ValidateProperty(value, item.Name);
                dp.NotityProperChanged(item.Name);
                if (strValid != string.Empty)
                {
                    return(false);
                }
            }
            return(true);
        }
Пример #4
0
        public static string ValidateProperty <MetadataType>(this ValidationViewModelBase dp, string propertyName)
        {
            if (string.IsNullOrEmpty(propertyName))
            {
                return(string.Empty);
            }

            var targetType = dp.GetType();

            if (targetType != typeof(MetadataType))
            {
                TypeDescriptor.AddProviderTransparent(
                    new AssociatedMetadataTypeTypeDescriptionProvider(targetType, typeof(MetadataType)), targetType);
            }

            var propertyValue = targetType.GetProperty(propertyName).GetValue(dp, null);

            return(dp.ValidateProperty(propertyValue, propertyName));
        }