如何在使用IDataErrorInfo
進行驗證時禁用/啓用按鈕?我使用GalaSoft light Framework使用MVVM
。在我的Model類中,我實現了IDataErrorInfo
來顯示錯誤消息。啓用在使用IDataErrorInfo進行驗證期間禁用保存按鈕
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;
}
}
截圖:http://i.stack.imgur.com/kwEI8.jpg
如何禁用/啓用保存按鈕。請建議?
感謝
這不是一種將驗證邏輯放入模型的好方法,因爲驗證邏輯可能會在不同的場景中發生變化。 –
爲簡單起見,它可以是公共布爾IsValid => ValidatedProperties.All(p => GetValidationError(p)== null);'。 – dee