编写用于禁用按钮的代码后,用于导航的按钮不起作用

时间:2013-09-24 05:14:00

标签: c# wpf validation mvvm

我有一个包含验证码的视图模型,还有一些代码用于在验证失败时禁用按钮。我的窗口包含员工信息的字段,该窗口包含一个保存按钮和一个用于导航到另一个窗口的按钮。

但是,在我编写了禁用按钮的代码后,该按钮无效。我已将'isenabled'属性绑定到用于禁用按钮的某个属性。我没有绑定到用于导航的按钮。为什么不工作?我正在使用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;
        }

这是我保存按钮的XAML:

<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>

这是我导航按钮的XAML:

 <Button Content="Home" Name="btnHome" Margin="440,0,-4,465" Command="{Binding HomeCommand}" Background="#FF2693A7" Foreground="White"  />

1 个答案:

答案 0 :(得分:1)

如果您说您的<Button Content="Home" />按钮被禁用,我看到的唯一原因是您已将命令绑定到该按钮,如果Command has CanExecute然后从CanExecute返回false则禁用按钮。