zoukankan      html  css  js  c++  java
  • WPF,数据验证方案,验证通过才能继续下一步

    上一篇总结,标题表达的不是很清楚~
    场景:用户填写表单,数据错误或者格式错误不能点击下一步按钮~

    ui上利用ValidRule去规正,下一步按钮利用Canexcute事件去控制用户能不能点击。
    (感觉实现比较繁琐,不知道各位大神有没有什么简单的方法)

    例子:填写IP地址
    Xmal:

      <TextBox Grid.Column="2" Height="26" >
                                        <Binding Path="Device.Communication.IP" UpdateSourceTrigger="PropertyChanged">
                                            <Binding.ValidationRules>
                                                <validRule:RequiredIPAddressValidRule ValidationStep="UpdatedValue" />
                                            </Binding.ValidationRules>
                                        </Binding>
                                    </TextBox>
    

    validRule:RequiredIPAddressValidRule:

        public class RequiredIPAddressValidRule : ValidationRule
    {
        public override ValidationResult Validate(object value, CultureInfo cultureInfo)
        {
            if (string.IsNullOrEmpty(ValidRulesHelper.GetBoundValue(value) as string))
            {
                return new ValidationResult(false, "required field.");
            }
            var temp = ValidRulesHelper.GetBoundValue(value) as string;
            bool isCorrect= Regex.IsMatch(temp, @"^((2[0-4]d|25[0-5]|[01]?dd?).){3}(2[0-4]d|25[0-5]|[01]?dd?)$");
            if (isCorrect)
            {
                return new ValidationResult(true, null);
            }
            else
            {
                return new ValidationResult(false, "incorrect ip address format.");
            }
        }
    }
      public static object GetBoundValue(object value)
        {
            if (value is BindingExpression)
            {
                // ValidationStep was UpdatedValue or CommittedValue (validate after setting)
                // Need to pull the value out of the BindingExpression.
                BindingExpression binding = (BindingExpression)value;
    
                // Get the bound object and name of the property
                string resolvedPropertyName = binding.GetType().GetProperty("ResolvedSourcePropertyName", BindingFlags.Public | BindingFlags.DeclaredOnly | BindingFlags.Instance).GetValue(binding, null).ToString();
    
                object resolvedSource = binding.GetType().GetProperty("ResolvedSource", BindingFlags.Public | BindingFlags.DeclaredOnly | BindingFlags.Instance).GetValue(binding, null);
    
                // Extract the value of the property
                object propertyValue = resolvedSource.GetType().GetProperty(resolvedPropertyName).GetValue(resolvedSource, null);
    
                return propertyValue;
            }
            else
            {
                return value;
            }
        }
    

    按钮:

       public DelegateCommand OKCommand =>
            _OKCommand ?? (_OKCommand = new DelegateCommand(ExecuteOKCommand,() => Check()));
        public RequiredNumberValidRule RequiredNumberValidRule { get; set; } 
        bool Check()
        {
            bool para1OK = RequiredNumberValidRule.Validate(Verify_Threshold, null).IsValid;
            bool para2OK = RequiredNumberValidRule.Validate(Verify_Correlation, null).IsValid;
            return para1OK && para2OK ;
        }
    

    属性通知更改:

       private double _Verify_Threshold = 1;
        /// <summary>
        /// 阈值
        /// </summary>
        public double Verify_Threshold
        {
            get { return _Verify_Threshold; }
            set 
            {
                SetProperty(ref _Verify_Threshold, value);
                OKCommand.RaiseCanExecuteChanged();
            }
        }
  • 相关阅读:
    本地通知
    常用颜色RGB、灰度值
    AFNetworkingErrorDomain 错误解决方法
    给iOS开发新手送点福利,简述UIPikerView的属性和用法
    给iOS开发新手送点福利,简述UIImagePickerController的属性和用法
    给iOS开发新手送点福利,简述UIDatePicker的用法
    iOS线程开发小结
    给iOS开发新手送点福利,简述文本属性Attributes的用法
    给iOS开发新手送点福利,简述UIScrollView的属性和用法
    给iOS开发新手送点福利,简述UITableView的属性和用法
  • 原文地址:https://www.cnblogs.com/swobble/p/13408170.html
Copyright © 2011-2022 走看看