IsEnabled property of RelayCommand not showing expected behavior to enable and disable the control on changing that property

May 1, 2011 at 8:49 PM

Here is solution that I came up with:

1) RelayCommand class declaration could be as:

public class RelayCommand<T> : ViewModelBase, ICommand


2) Implementation of IsEnabled property could be as follows:

        public bool IsEnabled
        {
            get { return _isEnabled; }
            set
            {
                if (value != _isEnabled)
                {
                    _isEnabled = value;

                    OnPropertyChanged("IsEnabled");
                }
            }
        }

3) Lastly you need to bind the IsEnabled property in xaml as follows:

IsEnabled="{Binding Path=SearchCommand.IsEnabled}"

May 4, 2011 at 4:25 AM

When you bind to an ICommand, it automatically takes care of the IsEnabled property. Don't bind that yourself.

Return your condition from the canExecute lambda expression (the second, optional constructor parameter). If you need to refresh the enabled state, call RaiseCanExecuteChanged on the command object.