using System; using System.Collections.Generic; using System.Globalization; using System.Windows.Controls; using System.Windows.Data; namespace ECMonitor.MVVM { public class VerifyRule : ValidationRule { public VerifyRuleType Type { get; set; } = VerifyRuleType.Unknown; public List WhatList { get; set; } public override ValidationResult Validate(object value, CultureInfo cultureInfo) { string strVal = value != null ? (string)value : string.Empty; ValidationResult vRet = Type switch { VerifyRuleType.NotEmpty => VerifyNotEmpty(strVal), VerifyRuleType.IP => VerifyIP(strVal), VerifyRuleType.Unknown => VerifyUnkown(strVal), VerifyRuleType.Temp => VerifyTemp(strVal), _ => VerifyUnkown(strVal), }; WhatList = new(); WhatList.Add(new { Type, vRet.IsValid, value, strVal, vRet }); return vRet; } #region Verify Region private static ValidationResult VerifyNotEmpty(string value) { bool ret = !string.IsNullOrWhiteSpace(value); string errMsg = "不能为空!"; return ret ? ValidationResult.ValidResult : new ValidationResult(false, errMsg); } private static ValidationResult VerifyIP(string value) { bool ret = VerifyUnit.CheckIp(value); string errMsg = "IP格式错误!"; return ret ? ValidationResult.ValidResult : new ValidationResult(false, errMsg); } private static ValidationResult VerifyTemp(string value) { bool ret = !string.IsNullOrWhiteSpace(value); string errMsg = "临时错误!"; return ret ? ValidationResult.ValidResult : new ValidationResult(false, errMsg); } private static ValidationResult VerifyUnkown(string value) { bool ret = false; string errMsg = "未知错误!"; return ret ? ValidationResult.ValidResult : new ValidationResult(false, errMsg); } #endregion Verify Region } }