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]?\d\d?)\.){3}(2[0-4]\d|25[0-5]|[01]?\d\d?)$");
        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();
        }
    }
posted @ 2020-07-31 10:17  猝不及防  阅读(336)  评论(0编辑  收藏  举报