【问题标题】:How can I enable/disable a button?如何启用/禁用按钮?
【发布时间】:2012-11-21 17:00:44
【问题描述】:

虽然我已经找到了这个问题的几个答案,但我还是不明白。所以请原谅我问。

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

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

命令使用RelayCommand。现在我想做以下事情:

  • 用户点击按钮,相应的命令被执行。这行得通。
  • 在此命令中,另一个按钮应被停用,即不可点击。

我发现使用CanExecute 应该可以做到这一点,但说实话:我根本不明白。我可以将按钮设置为启用/禁用吗?

这是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;
    }
}

【问题讨论】:

  • 你能显示ExecuteCanExecute这两个命令的代码吗?
  • 添加了命令用法。但实际上,我没有 ExecuteCanExecute 的代码,除了 RelayCommand 类中的代码。
  • 谢谢罗伯特。 Button 的 Enabled/Disabled 自动绑定到 Command.CanExecute,因此您需要让 Button1 设置一些东西,使 CanButton2Execute 在运行时等于 false,然后 Button2 将被禁用。

标签: c# wpf mvvm controls


【解决方案1】:

在创建 RelayCommand 对象时,您必须传递一个 can Predicate,它可以是(除其他外)具有以下签名的方法:

bool MethodName(对象参数)。

如果您不需要该参数,请使用例如bool MethodName(),但是像这样将它传递给 RelayCommand 构造函数:(o) => MethodName()。

在此方法中,您应该执行您的逻辑并返回一个值,指示该命令是否可以执行。其余的应由 WPF Command 基础架构处理。

【讨论】:

  • 忘记指定:将谓词作为构造函数中的第二个参数传递:new RelayCommand(param => this.ProjectLogin(), param => this.MethodName())
  • 谢谢,这条评论与 emybobs 的回答相结合,它是正确的 :-)
【解决方案2】:

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

它如何影响您绑定命令的按钮,它是否会持续运行布尔方法,当它返回 false 时,命令绑定的按钮将被禁用。

所以在你的命令属性中:

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
}

如果你在 bool 方法中放置一个断点,你可以看到 CanExecute 是如何工作的。

【讨论】:

  • 这个解释让我可以理解 :-) 但是,第二个参数缺少 param => 语句。
【解决方案3】:

如果您在使用 canExecute 回调时遇到问题,您可能会发现使用更简单的 RelayCommand 版本会更容易:

class RelayCommand : ICommand 
{
    readonly Action execute;
    private bool canExecute;

    public RelayCommand(Action execute) 
    {
        this.execute = execute;
        this.canExecute = true;
    }

    public bool CanExecute(object parameter)
    {
        return canExecute;
    }

    public void SetCanExecute(bool canExecute)
    {
        this.canExecute = canExecute;
        var handler = CanExecuteChanged;
        if (handler != null) handler(this, EventArgs.Empty);
    }

    public event EventHandler CanExecuteChanged;

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

使用这种方法,您可以保存对您创建的 RelayCommand 对象的引用,从而可以禁用如下命令:

getProjectListCommand.SetCanExecute(false);

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2017-11-07
    • 1970-01-01
    • 2013-10-13
    • 2015-05-05
    • 2021-05-18
    • 2010-09-05
    • 2011-12-16
    相关资源
    最近更新 更多