Alex Rivera | Logout

CommandManager.InvalidateRequerySuggested does not cause a requery on CanExecute in MVVM-Light

Asked 2011-05-17T09:04:40.250
12

I am using MVVM-Light RelayCommand

private ICommand myRevertCmd;
public ICommand Revert
    {
        get
        {
            if (myRevertCmd == null)
            {
                myRevertCmd = new RelayCommand(RevertExecute, CanRevertExecute);
            }

            return myRevertCmd;
        }
    }

    private void RevertExecute()
    {
        searchType = SearchType.Revert;
        SearchStart();
    }

    private bool CanRevertExecute()
    {
        return isRevertEnabled;
    }

I have some code that changes the value of isRevertEnabled but the linked button does not change. After some searching I found that you can use to force the re-evaluation of the button states

// force the GUI to re-evaluate the state of the buttons
CommandManager.InvalidateRequerySuggested();

But this doesn't work. Does any one have any suggestions?

Edit
Report

2 Answers

46

Just to add another possible solution, in my case I needed to call CommandManager.InvalidateRequerySuggested on the UI thread using Application.Current.Dispatcher.Invoke.

answered 2013-04-05T08:05:15.873
7

There are a lot suggestions out there (here, here, here).

I use a simple but not so beautiful workaround. I simply call OnPropertyChanged("MyICommand") for my commands in my BackgroundWorker Completed Event.

answered 2011-05-17T10:45:18.390

Your Answer