XiChang/DM_Weight/Common/TemperatureRangeRule.cs

69 lines
2.4 KiB
C#
Raw Normal View History

2024-02-27 09:01:14 +08:00
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);
2024-02-27 09:01:14 +08:00
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))
2024-02-27 09:01:14 +08:00
{
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);
}
}
}
}