• 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();
            }
        }
  • 相关阅读:
    监控LVS
    技巧:结合Zabbix与SNMP监控嵌入式设备
    Vmware Exsi使用简要说明
    (转)Linux LVM逻辑卷配置过程详解(创建、扩展、缩减、删除、卸载、快照创建)
    Linux系统下减少LV(逻辑卷)容量
    Linux系统下增加LV(逻辑卷)容量 、Linux系统下减少LV(逻辑卷)容量
    yarn命令删除job
    mr自定义排序和分类
    mr利用shuffle阶段来实现数据去重的功能
    hadoop如何使用第三方依赖jar包(转载)
  • 原文地址:https://www.cnblogs.com/swobble/p/13408170.html
Copyright © 2020-2023  润新知