TRS
TRS

Reputation: 2097

CanExecute on Relay Command not working

I have a simple program with a button which is binded to a Relaycommand in View model as below.I am setting the CanExecute to true based on some value (set by a timer you can find details below).My button is enabled when Status is 3,initially as I set it in constructor.But it doesn't get disabled when Status value changes on it own.I can see that disabling only when I click on it.can anybody explain why it doesn't disable on its own

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));
    }
}

My realy command implementation is

 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
}

Upvotes: 1

Views: 1559

Answers (2)

TRS
TRS

Reputation: 2097

Thanks for your inputs ,I have solved my problem by adding CommandManager.InvalidateRequerySuggested in my relaycommand implementation.

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

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

Upvotes: 1

Mashton
Mashton

Reputation: 6415

In the same way that you raise PropertyChanged events, you can raise CanExecuteChanged events ... ClickButtonCommand.RaiseCanExecuteChanged();

Upvotes: 0

Related Questions