Example #1
0
        /// <summary>
        /// 验证ViewModel的某一个属性的有效性
        /// </summary>
        /// <param name="dp"></param>
        /// <param name="propertyName"></param>
        /// <returns></returns>
        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));
        }
Example #2
0
        /// <summary>
        /// 验证整个ViewModel的有性性
        /// </summary>
        /// <param name="dp"></param>
        /// <returns></returns>
        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);
        }
Example #3
0
        /// <summary>
        /// 验证ViewModel的某一个属性的有效性
        /// </summary>
        /// <param name="dp">ViewModel</param>
        /// <param name="value">值</param>
        /// <param name="propertyName">属性名称</param>
        /// <returns></returns>
        public static string ValidateProperty(this ValidationViewModelBase dp, object value, string propertyName)
        {
            ValidationContext vc = new ValidationContext(dp, null, null)
            {
                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);
            }
        }
Example #4
0
        /// <summary>
        /// 验证ViewModel的某一个属性的有效性
        /// </summary>
        /// <typeparam name="MetadataType"></typeparam>
        /// <param name="dp"></param>
        /// <param name="propertyName"></param>
        /// <returns></returns>
        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));
        }