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

2021-06-28 16:04

阅读:382

标签:property   max   res   regex   result   wpf   tostring   textbox   shared   

正则:^(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
    }

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

标签:property   max   res   regex   result   wpf   tostring   textbox   shared   

原文地址:https://www.cnblogs.com/wangyan89smile/p/10037145.html


评论


亲,登录后才可以留言!