n0kx
n0kx

Reputation: 81

Trying to update CanExecute for my commands

I've created a base view model that implements ICommand. The commands bind just fine, execute as expected, and even begin in the correct states, but as properties that influence whether a command can execute or not change, the CanExecute for those commands doesn't seem to be updating.

In my code below, I can click the Run button and everything works as expected EXCEPT for the fact that when the ProgramStatus changes to Running it should be disabled.

In my base view model:

public class RelayCommand : ICommand
    {

        #region ICommand Member Variables

        private Action<object> _execute;
        private Predicate<object> _canExecute;
        private event EventHandler _canExecuteChangedInternal;

        #endregion // ICommand Member Variables

        #region Constructors

        public RelayCommand(Action<object> execute, Predicate<object> canExecute)
        {
            _execute = execute;
            _canExecute = canExecute;
        }

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

        }

        #endregion // Constructors

        #region ICommand Members

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

            remove
            {
                CommandManager.RequerySuggested -= value;
                _canExecuteChangedInternal -= value;
            }
        }

        public bool CanExecute(object parameter)
        {
            return _canExecute != null && _canExecute(parameter);
        }

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

        public void OnCanExecuteChanged()
        {
            EventHandler eventHandler = _canExecuteChangedInternal;

            if (eventHandler != null)
            {
                eventHandler.Invoke(this, EventArgs.Empty);
            }
        }

        private static bool DefaultCanExecute(object parameter)
        {
            return true;
        }

        #endregion // ICommand Members

In my view model:

RelayCommand _runCommand;
    public RelayCommand RunCommand
    {
        get
        {
            if (_runCommand == null)
            {
                _runCommand = new RelayCommand(param => Run(), param => CanRun);
            }

            return _runCommand;
        }
    }

public bool CanRun
    {
        get
        {
            bool result = false;

            if (Machine.ProgramStatus != ProgramStatus.Running && Machine.ProgramStatus != ProgramStatus.TestRunning)
            {
                result = true;
            }

            return result;
        }
    }

In my view:

<Button Content="Run" Command="{Binding Path=RunCommand}" />

Upvotes: 0

Views: 404

Answers (1)

Wes
Wes

Reputation: 385

You have to invoke the CanExecuteChanged EventHandler. So when ProgramStatus changes to Running you can call the OnCanExecuteChanged helper method for the command you want to update (e.g. RunCommand.OnCanExecuteChanged();).

If CanExecute is bound to a property you can use the OnCanExecuteChanged helper method in the property's setter.

 private bool _myProperty;
 public bool MyProperty
    {
        get { return _myProperty; }
        set
        {
            _myProperty= value;
            RunCommand.OnCanExecuteChanged();
        }
    }

Upvotes: 1

Related Questions