Я пишу страницу настроек для установки IP-адреса для устройства Ethernet.
Я разместил на странице текстовое поле и кнопку «Применить». После ввода IP-адреса в текстовое поле и нажатия кнопки «Применить», затем установите IP-адрес для устройства.
Теперь мне нужно проверить текст в текстовом поле, является ли это IP-адресом.
Я нашел руководство в
https://learn.microsoft.com/en-us/dotne ... attributes
< br />
Вот мой код:
Код: Выделить всё
public partial class EthernetConnection : ObservableObject
{
string _IP = "";
[IPAddress]
public string IP {
get => _IP;
set {
if (_IP != value)
{
_IP = value;
OnPropertyChanged();
}
}
}
public sealed class IPAddressAttribute : ValidationAttribute
{
protected override ValidationResult IsValid(object value, ValidationContext validationContext)
{
string _pattern = @"^((25[0-5]|2[0-4]\d|[01]?\d\d?)\.){3}(25[0-5]|2[0-4]\d|[01]?\d\d?)$";
if (value is string _string && Regex.IsMatch(_string, _pattern))
{
return ValidationResult.Success;
}
return new("Failed");
}
}
}
But now I have encountered a problem. When clicking the Apply Button, I have to write additional code to recheck whether the text in the TextBox is an IP address, which is very troublesome.
Is there a way for me to directly detect if a property has been validated?
Источник: https://stackoverflow.com/questions/781 ... -validated