我有一個視圖模型,其中包含驗證代碼,並且還有一些驗證失敗時用於禁用按鈕的代碼..我有窗口包含員工信息的字段,該窗口包含保存按鈕和一個按鈕導航到另一個窗口..但該按鈕不起作用後,我編寫代碼禁用按鈕..我已經綁定了'isenabled'屬性的一些屬性,用於禁用按鈕..我沒有結合用於navigation.why它不工作?按鈕..幫我urgently..am使用MVVM用於導航的按鈕編寫代碼禁用後不工作按鈕
這是我失效按鈕
public EmployeeViewModel()
{
this.validProperties = new Dictionary<string, bool>();
this.validProperties.Add("FirstName", false);
this.validProperties.Add("LastName", false);
this.validProperties.Add("Street1", false);
this.validProperties.Add("Street2", false);
this.validProperties.Add("City", false);
this.validProperties.Add("State", false);
this.validProperties.Add("ZipCode", false);
this.validProperties.Add("PhoneNumber", false);
this.validProperties.Add("MobileNumber", false);
this.validProperties.Add("Email", false);
this.validProperties.Add("Web", false);
}
public bool AllPropertiesValid
{
get { return allPropertiesValid; }
set
{
if (allPropertiesValid != value)
{
allPropertiesValid = value;
OnPropertyChanged("AllPropertiesValid");
}
}
}
private void ValidateProperties()
{
foreach (bool isValid in validProperties.Values)
{
if (!isValid)
{
this.AllPropertiesValid = false;
return;
}
}
this.AllPropertiesValid = true;
}
public string Error
{
get { throw new NotImplementedException(); }
}
public string this[string propertyName]
{
get
{
string strMessage = string.Empty;
validateUserInput(ref strMessage, propertyName);
validProperties[propertyName] = String.IsNullOrEmpty(strMessage) ? true : false;
ValidateProperties();
CommandManager.InvalidateRequerySuggested();
return strMessage;
}
}
private string validateUserInput(ref string pstrMessage, string pstrpropertyName)
{
switch (pstrpropertyName)
{
case "FirstName":
if (string.IsNullOrEmpty(FirstName))
pstrMessage = "FirstName is required.";
else if (string.IsNullOrWhiteSpace(FirstName))
pstrMessage = "Spaces are not allowed in First name. only character are allowed";
break;
case "LastName":
if (string.IsNullOrEmpty(LastName))
pstrMessage = "LastName is required.";
break;
case "Street1":
if (string.IsNullOrEmpty(Street1))
pstrMessage = "Street1 is required";
break;
case "Street2":
if (string.IsNullOrEmpty(Street2))
pstrMessage = "Street2 is required";
break;
case "City":
if (string.IsNullOrEmpty(City))
pstrMessage = "City is required";
break;
case "State":
if (string.IsNullOrEmpty(State))
pstrMessage = "State is required";
break;
case "ZipCode":
if (string.IsNullOrEmpty(ZipCode))
pstrMessage = "ZipCode is required";
else if (Regex.IsMatch(employee.ZipCode, AppConstants.Regexpatterns.ZiCodeRegex) == false)
pstrMessage = "Only 6 digits are allowed in ZipCode field.";
break;
case "PhoneNumber":
if (string.IsNullOrEmpty(PhoneNumber))
pstrMessage = "PhoneNumber is required";
else if (Regex.IsMatch(employee.PhoneNumber, AppConstants.Regexpatterns.PhoneNumberRegex) == false)
pstrMessage = "Enter a valid PhoneNumber.";
break;
case "MobileNumber":
if (string.IsNullOrEmpty(MobileNumber))
pstrMessage = "MoblieNumber is required";
else if (Regex.IsMatch(employee.MobileNumber, AppConstants.Regexpatterns.PhoneNumberRegex) == false)
pstrMessage = "Enter a valid MobileNumber.";
break;
case "Email":
if (!(string.IsNullOrEmpty(Email)))
if (Regex.IsMatch(Email, AppConstants.Regexpatterns.EmailRegex) == false)
pstrMessage = "Enter a valid EmailID.";
break;
case "Web":
if (!(string.IsNullOrEmpty(Web)))
if (Regex.IsMatch(Web, AppConstants.Regexpatterns.WebRegex) == false)
pstrMessage = "Enter a valid Web Address.";
break;
}
return pstrMessage;
}
012碼
這是我保存按鈕
<Button Grid.Row="11" Grid.Column="3" Width="47" Name="btnAdd" Content="ADD" HorizontalAlignment="Left" VerticalAlignment="Top" Margin="0,6,0,0" Height="22" Background="Black" Foreground="White" Command="{Binding SaveEmployeeCommand}" IsEnabled="{Binding Path=AllPropertiesValid}"></Button>
this is my xaml for navigate button
<Button Content="Home" Name="btnHome" Margin="440,0,-4,465" Command="{Binding HomeCommand}" Background="#FF2693A7" Foreground="White" />
你使用的是什麼類型的命令和MVVM框架?命令的某些實現(例如MvvmLight中的DelegateCommand)提供了「可執行」邏輯,您可以使用它來禁用按鈕。此外,您可能需要考慮使用數據註釋或業務對象/規則框架,這使得此驗證在您身上更容易。也就是說,在這種情況下它不能很好地工作,因爲你的OnPropertyChanged看起來不錯。 (注意:你可以在.NET 4.5中的OnPropertyChanged參數上使用屬性[CallerMemberName]來避免魔法字符串。) – Guttsy
你在HomeCommand() –