Exemplo n.º 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));
        }
Exemplo n.º 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);
        }
Exemplo n.º 3
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));
        }