WPF TextBox 正则验证 大于等于0 小于等于1 的两位小数

正则:^(0\.\d+|[1-9][0-9]|1)$

TextBox绑定正则验证


   
                  Mode="TwoWay" UpdateSourceTrigger="PropertyChanged"                              >
           
               
           

      

   

 

用到的InventoryValidationRule类:

 public class InventoryValidationRule : ValidationRule
    {
        #region Properties

        public string InventoryPattern { get; set; }

        #endregion Properties

        #region Methods

        public override ValidationResult Validate(
                  object value, CultureInfo cultureInfo)
        {
            if (InventoryPattern == null)
                return ValidationResult.ValidResult;

            if (!(value is string))
                return new ValidationResult(false,
               "Inventory should be a comma separated list of model numbers as a string");

            string[] pieces = value.ToString().Split(',');
            Regex m_RegEx = new Regex(InventoryPattern);

            foreach (string item in pieces)
            {
                Match match = m_RegEx.Match(item);
                if (match == null || match == Match.Empty)
                    return new ValidationResult(
                      false, "Invalid input format");
            }

            return ValidationResult.ValidResult;
        }

        #endregion Methods
    }

转载于:https://www.cnblogs.com/wangyan89smile/p/10037145.html

你可能感兴趣的:(WPF TextBox 正则验证 大于等于0 小于等于1 的两位小数)