Jak wyłączyć/włączyć przycisk podczas sprawdzania poprawności za pomocą IDataErrorInfo
?Włącz Wyłącz przycisk zapisywania podczas sprawdzania poprawności za pomocą IDataErrorInfo
Używam MVVM
przy użyciu GalaSoft light Framework. W mojej klasie modelu zaimplementowałem IDataErrorInfo
, aby wyświetlić komunikaty o błędach.
public string this[string columnName]
{
get
{
Result = null;
if (columnName == "FirstName")
{
if (String.IsNullOrEmpty(FirstName))
{
Result = "Please enter first name";
}
}
else if (columnName == "LastName")
{
if (String.IsNullOrEmpty(LastName))
{
Result = "Please enter last name";
}
}
else if (columnName == "Address")
{
if (String.IsNullOrEmpty(Address))
{
Result = "Please enter Address";
}
}
else if (columnName == "City")
{
if (String.IsNullOrEmpty(City))
{
Result = "Please enter city";
}
}
else if (columnName == "State")
{
if (State == "Select")
{
Result = "Please select state";
}
}
else if (columnName == "Zip")
{
if (String.IsNullOrEmpty(Zip))
{
Result = "Please enter zip";
}
else if (Zip.Length < 6)
{
Result = "Zip's length has to be at least 6 digits!";
}
else
{
bool zipNumber = Regex.IsMatch(Zip, @"^[0-9]*$");
if (zipNumber == false)
{
Result = "Please enter only digits in zip";
}
}
}
else if (columnName == "IsValid")
{
Result = true.ToString();
}
return Result;
}
}
Zrzut ekranu: http://i.stack.imgur.com/kwEI8.jpg
Jak wyłączyć/włączyć przycisk Zapisz. Uprzejmie sugerujesz?
Dzięki
To nie jest dobre podejście do umieszczenia logiki walidacyjnej w modelu, ponieważ logika walidacji może się zmienić w innym scenariuszu. –
Dla uproszczenia może to być po prostu 'public bool IsValid => ValidatedProperties.All (p => GetValidationError (p) == null);'. – dee