using System.Globalization; using System.Windows.Controls; namespace PortProxyTool.Wpf.Rules; public class PortValidationRule : ValidationRule { public override ValidationResult Validate(object? value, CultureInfo cultureInfo) { if (value is not string strValue) return new ValidationResult(false, "请输入有效的数字"); if (string.IsNullOrEmpty(strValue) || ushort.TryParse(strValue, out _)) return ValidationResult.ValidResult; return new ValidationResult(false, "请输入有效的数字"); } }