【问题标题】:WPF Update button state from inside commandWPF 从内部命令更新按钮状态
【发布时间】:2012-05-05 20:38:24
【问题描述】:

视图上有不同的按钮,当一个命令被执行时,我如何控制按钮的 IsEnabled 状态?

如果我执行这个命令:

public ICommand DoSomethingCommand
{
  get
  {
    if (doSomethingCommand == null)
    {
      doSomethingCommand = new RelayCommand(
        (parameter) =>
          {
            this.IsBusy = true;
            this.someService.DoSomething(
              (b, m) =>
                {
                  this.IsBusy = false;
                }
              );
          },
        (parameter) => !this.IsBusy);
    }
    return this.doSomethingCommand ;
  }
}

我设置了触发 OnPropertyChanged 事件的 IsBusy 属性。所有其他按钮在其命令的 CanExecute 中检查此属性。但是执行上述命令时,按钮不会被禁用。

我该怎么做?

【问题讨论】:

    标签: wpf button command


    【解决方案1】:

    CanExecuteChanged 需要触发,以便命令源知道调用 CanExecute。

    一个简单的方法是调用CommandManager.InvalidateRequerySuggested()。

    bool _isBusy;
    public bool IsBusy
    {
        get { return _isBusy; }
        set
        {
            if (value == _isBusy)
                return;
            _isBusy = value;
            //NotifyPropertyChanged("IsBusy"); // uncomment if IsBusy is also a Dependency Property
            System.Windows.Input.CommandManager.InvalidateRequerySuggested();
        }
    }
    

    【讨论】:

    • 可能不是,但您可以将调用移至this.IsBusy = false; 之后的InvalidateRequerySuggested,这应该使其更像MVVM。
    【解决方案2】:

    为什么不在您自己的 RelayCommand 类的自定义版本上实现 INotifyPropertyChanged。这将符合 MVVM:

    public class RelayCommand : ICommand, INotifyPropertyChanged
    {
        public event PropertyChangedEventHandler PropertyChanged;
        private readonly Action execute;
        private readonly Func<bool> canExecute;
    
        private bool isBusy;
        public bool IsBusy
        {
            get { return isBusy; }   
            set
            {
                isBusy = value;
                var handler = PropertyChanged;
                if (handler != null)
                {
                    handler(this, new PropertyChangedEventArgs("IsBusy"));
                }
            }
        }
    
        public event EventHandler CanExecuteChanged
        {
            add
            {
                if (this.canExecute != null)
                {
                    CommandManager.RequerySuggested += value;
                }
            }
            remove
            {
                if (this.canExecute != null)
                {
                    CommandManager.RequerySuggested -= value;
                }
            }
        }
    
        public RelayCommand(Action execute) : this(execute, null)
        {
        }
    
        public RelayCommand(Action execute, Func<bool> canExecute)
        {
            if (execute == null)
            {
                throw new ArgumentNullException("execute");
            }
            this.execute = execute;
            this.canExecute = canExecute;
        }
    
        public void Execute(object parameter)
        {
            this.IsBusy = true;
            this.execute();
            this.IsBusy = false;
        }
    
        public bool CanExecute(object parameter)
        {
            return this.canExecute == null ? true : this.canExecute();
        }
    }
    

    然后使用这样的命令:

    <Button Command="{Binding Path=DoSomethingCommand}" 
            IsEnabled="{Binding Path=DoSomethingcommand.IsBusy, 
                Converter={StaticResource reverseBoolConverter}}" 
            Content="Do It" />
    

    【讨论】:

    • 为什么要绑定IsEnabled?在绑定到的命令上调用 CanExecute() 时,按钮不应该自行更改吗?
    猜你喜欢
    • 1970-01-01
    • 2013-05-29
    • 2020-07-25
    • 1970-01-01
    • 1970-01-01
    • 2013-10-13
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多