using System; using System.Collections.Generic; using System.Configuration; using System.Globalization; using System.Linq; using System.Text; using System.Threading.Tasks; using System.Windows.Controls; using System.Windows.Data; namespace DM_Weight.Common { //设置冰箱温度规则 public class TemperatureRangeRule : ValidationRule { //冰箱温度设置区间为取自配置文件(2~8度) public override ValidationResult Validate(object value, CultureInfo cultureInfo) { bool flag = false; string tips = string.Empty; try { string[] rang = value.ToString().Split('-'); if (rang.Length >= 2) { bool bSRange = float.TryParse(rang[0], out float sRange); bool bERange = float.TryParse(rang[1], out float eRange); if (bSRange && bERange) { string[] sList = sRange.ToString().Split('.'); string[] eList = eRange.ToString().Split("."); if ((sList.Length > 1 && sList[1].Length > 1) || (eList.Length > 1 && eList[1].Length > 1)) { tips = "小数点后保留1位"; return new ValidationResult(flag, tips); } if ((sRange < 2 || eRange > 8 || sRange > 8 || eRange < 2)) { tips = "温度区间设置2-8度,请检查输入"; return new ValidationResult(flag, tips); } else { flag = true; } } else { tips = "请输入正确的数值"; return new ValidationResult(flag, tips); } } else { tips = "请输入正确的数值"; return new ValidationResult(flag, tips); } return new ValidationResult(flag, tips); } catch (Exception ex) { tips = $"校验异常{ex.ToString()}"; return new ValidationResult(flag, tips); } } } }