如何启用/禁用按钮? [英] How can I enable/disable a button?

查看:157
本文介绍了如何启用/禁用按钮?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

尽管我已经找到了该问题的几个答案,但我还是不知所措。因此,请原谅。

Although I have found several answers to this question I somehow don't get it. So please excuse me asking.

我有遵循MVVM模式的WPF应用程序。它包含一个与视图模型中的命令绑定的按钮:

I have a WPF application following the MVVM pattern. It contains a button which is bound to a command in the view model:

< button Content = Login Command = {Binding ProjectLoginCommand } />

命令正在使用 RelayCommand 。现在,我想执行以下操作:

The commands are using RelayCommand. Now I would like to do the following:


  • 用户单击按钮并执行相应的命令。

  • 在此命令中,另一个按钮应被停用,即该按钮不可单击。

我发现可以使用 CanExecute 做到这一点,但是老实说:我根本不了解。我可以将按钮设置为启用/禁用吗?

I found that this should be possible using CanExecute but being honest: I simply don't get it. Hoe can I set the button to enabled/disabled?

这是 RelayCommand.cs

namespace MyApp.Helpers {
    class RelayCommand : ICommand {

    readonly Action<object> execute;
    readonly Predicate<object> canExecute;

    public RelayCommand(Action<object> execute) : this(execute, null) {
    }

    public RelayCommand(Action<object> execute, Predicate<object> canExecute)
    {
      if (execute == null)
          throw new ArgumentNullException("execute");

      this.execute = execute;
      this.canExecute = canExecute;           
    }

    public bool CanExecute(object parameter)
    {
      return canExecute == null ? true : canExecute(parameter);
    }

  public event EventHandler CanExecuteChanged
  {
      add { CommandManager.RequerySuggested += value; }
      remove { CommandManager.RequerySuggested -= value; }
  }

  public void Execute(object parameter)
  {
      execute(parameter);
  }
 }
}

这就是我所说的命令:

RelayCommand getProjectListCommand;

public ICommand GetProjectListCommand {
    get {
        if (getProjectListCommand == null) {
            getProjectListCommand = new RelayCommand(param => this.ProjectLogin());
        }
        return getProjectListCommand;
    }
}


推荐答案

何时您可以使用RelayCommand来指定两种方法。第一种方法是调用命令时要运行的主要方法。用于添加检查(例如验证)的第二种方法,这应该返回布尔值。如果返回false,则main方法将无法运行。

When you use the RelayCommand you can specify two methods. The first method is the main method you want to run when the command is invoked. The second method you use to add in checks such as validation, and this should return a bool. if it returns false, then the main method will not run.

它如何影响绑定到命令的按钮,它将持续运行boolean方法,并且当返回false时,命令绑定到的按钮将被禁用。

How it affects the button you have the command bound to, is it will run the boolean method continually, and while it return false, then the button the command is bound to will be disabled.

因此在命令属性中:

public ICommand GetProjectListCommand {
get {
    if (getProjectListCommand == null) {
        getProjectListCommand = new RelayCommand(param => this.ProjectLogin(), CanProjectLogin());
    }
    return getProjectListCommand;
}

添加新方法:

public bool CanProjectLogin()
{
    //here check some properties to make sure everything is set that you'd want to use in your ProjectLogin() method
}

您可以看到如果在其中放置一个断点,则CanExecute的工作原理布尔方法。

you can see how the CanExecute works if you put a break point in the bool method.

这篇关于如何启用/禁用按钮?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆