-1

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

但是,在我编写了禁用按钮的代码后,该按钮不起作用。我已将“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"  />
4

1 回答 1

1

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

于 2013-09-24T05:22:19.430 回答