我有一个简单的程序,其中包含一个按钮,该按钮绑定(bind)到View模型中的RelayCommand上,如下所示。我将CanExecute基于某个值(由计时器设置,可以在下面找到详细信息)设置为true。当Status为3,最初是我在构造函数中设置的。但是当状态值自行更改时它不会被禁用。我只能在单击它时看到禁用。任何人都可以解释为什么它不会自己禁用

public class MainWindowViewModel : INotifyPropertyChanged
{

    private RelayCommand mClickButtonCommand;
    private int mStatus;
    private Timer mTimer;


    public MainWindowViewModel()
    {
        Status = 3;

        mTimer = new Timer(1000);
        mTimer.Elapsed += OnElapsed;
        mTimer.Start();
    }

    private void OnElapsed(object sender, ElapsedEventArgs e)
    {
        if (Status == 5)
        {
            Status = 0;
        }
        Status++;
    }

    public ICommand ClickButtonCommand
    {
        get
        {
            if (mClickButtonCommand == null)
            {
                mClickButtonCommand = new RelayCommand(OnClick, () => CanClick);
            }
            return mClickButtonCommand;
        }
    }

    private void OnClick()
    {
        Console.WriteLine("Clicked");
    }

    public bool CanClick
    {
        get { return Status == 3; }
    }

    public int Status
    {
        get { return mStatus; }
        set
        {
            mStatus = value;
            OnPropertyChanged("Status");
            OnPropertyChanged("CanClick");
        }
    }

    public event PropertyChangedEventHandler PropertyChanged;

    [NotifyPropertyChangedInvocator]
    protected virtual void OnPropertyChanged(string propertyName)
    {
        PropertyChangedEventHandler handler = PropertyChanged;
        if (handler != null) handler(this, new PropertyChangedEventArgs(propertyName));
    }
}

我真正的命令实现是
 public class RelayCommand : ICommand
{



    public RelayCommand(Action execute)
        : this(execute, null)
    {
    }
    public RelayCommand(Action execute, Func<bool> canExecute)
    {
        if (execute == null)
            throw new ArgumentNullException("execute");

        _execute = execute;
        _canExecute = canExecute;
    }

    #region ICommand Members

    [DebuggerStepThrough]
    public bool CanExecute(object parameter)
    {
        return _canExecute == null ? true : _canExecute();
    }

    public event EventHandler CanExecuteChanged
    {
        add
        {
            if (_canExecute != null)
                CommandManager.RequerySuggested += value;
        }
        remove
        {
            if (_canExecute != null)
                CommandManager.RequerySuggested -= value;
        }
    }

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

    #endregion // ICommand Members

    #region Fields

    readonly Action _execute;
    readonly Func<bool> _canExecute;

    #endregion // Fields
}

最佳答案

感谢您的投入,我在中继命令实现中添加了CommandManager.InvalidateRequerySuggested解决了我的问题。

[DebuggerStepThrough]
    public bool CanExecute(object parameter)
    {
        bool result = true;

        if (_canExecute != null)
        {
            result = _canExecute();
            CommandManager.InvalidateRequerySuggested();
        }
        return result;
    }

关于c# - CanExecute on Relay Command不起作用,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/24119185/

10-13 06:02