11

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?

Andrea
  • 273
  • 1
  • 3
  • 8
  • 3
    Currently, there's an implementation of `RelayCommand` in an additional namespace: `GalaSoft.MvvmLight.CommandWpf` that specifically solves this issue! – heltonbiker Nov 11 '15 at 18:57
  • 1
    I tried all the answers below, nothing worked. But using GalaSoft.MvvmLight.CommandWpf.RelayCommand instead solved it for me. Thank you @heltonbiker – Paw Baltzersen Nov 24 '16 at 07:29

7 Answers7

43

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

Jens
  • 25,229
  • 9
  • 75
  • 117
  • This is what I needed to do – Jon Barker Oct 10 '14 at 13:55
  • 7
    +1 for the hint `on the UI thread`. I called it on a background thread and nothing happened – Flat Eric Oct 27 '14 at 15:36
  • 2
    With I could vote this up again. I wonder why this isn't done by default inside MVVMLight – Benjol Mar 31 '15 at 06:36
  • 1
    Do you place this code in ViewModel or on background code of the view? Sorry I am not able to get how to call it on UI thread? – Emil Jun 23 '16 at 14:39
  • I placed this call in the ViewModel. Application.Current.Dispatcher.Invoke takes a delegate that it runs on the UI thread. See the MSDN for more info. – Jens Jun 24 '16 at 06:38
  • I tried a bunch of other options/suggestions. This was the first one to work for me. yay. – steve Nov 22 '22 at 14:18
  • @Emil: I'm using a task in the view-model which is background but you might be using a background thread and maybe in another class. ... but for this to be useful it needs to work from the background code ... and it does for me. I call InvalidateRequerySuggested via the dispatcher in the task code. – steve Nov 22 '22 at 14:21
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.

Community
  • 1
  • 1
blindmeis
  • 22,175
  • 7
  • 55
  • 74
  • 3
    It may be a "not so beautiful workaround", but the OnPropertyChanged(Command) did just the trick for me. Thanks for this idea. I never would have thought of that. – Zamotic Mar 07 '12 at 00:00
  • If the UI is not "heavy", you can call `OnPropertyChanged(null)` to update _every_ property, including the commands (which will re-evaluate the CanExecute commands) – heltonbiker Nov 11 '15 at 18:43
4

According to Josh Smith's article 'Allowing CommandManager to query your ICommand objects'. The problem is that the command is a non-routed command.

I have made a new implementation of the MVVM-Light RelayCommand as follows:

// original
//public event EventHandler CanExecuteChanged;


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

public void RaiseCanExecuteChanged()
{
    CommandManager.InvalidateRequerySuggested();
    //            var handler = CanExecuteChanged;
    //            if (handler != null)
    //            {
    //                handler(this, EventArgs.Empty);
    //            }
}
akjoshi
  • 15,374
  • 13
  • 103
  • 121
Andrea
  • 273
  • 1
  • 3
  • 8
  • 2
    Currently, there's an implementation of `RelayCommand` in another namespace: `GalaSoft.MvvmLight.CommandWpf` that specifically solves this issue! – heltonbiker Nov 11 '15 at 18:57
0

Just call Revert.RaiseCanExecuteChanged();

O.O
  • 11,077
  • 18
  • 94
  • 182
0

I guess you can call "Revert.RaiseCanExecuteChanged()" method at "isRevertEnabled" INPC (or dependency property) set method. This will force the "CanRevertExecute" predicate.

akjoshi
  • 15,374
  • 13
  • 103
  • 121
Bradman
  • 200
  • 6
  • This creates a compile error: 'The event System.Windows.Input.ICommand.CanExecuteChanged can only appear on the left hand side of += or -=' – Andrea May 19 '11 at 15:26
0

I would turn the isRevertEnabled flag into a property and call the OnPropertyChanged method from there (you need to implement the INotifyPropertyChanged interface). At the point where you change the flag, you need to use the property, e.g. IsRevertEnabled = true.

private bool isRevertEnabled;

public bool IsRevertEnabled
{
    get
    {
        return isRevertEnabled;
    }
    set
    {
        if (isRevertEnabled != value)
        {
            isRevertEnabled = value;
            OnPropertyChanged("IsRevertEnabled");
        }
    }
}

private bool CanRevertExecute()     
{         
    return IsRevertEnabled;     
}
Julien Poulin
  • 12,737
  • 10
  • 51
  • 76
0

@heltonbiker already pointed this out in comments, but the solution is to change namespaces.

If you look at the source for RelayCommand, you see the following remark:

// Remarks:
//     If you are using this class in WPF4.5 or above, you need to use the GalaSoft.MvvmLight.CommandWpf
//     namespace (instead of GalaSoft.MvvmLight.Command). This will enable (or restore)
//     the CommandManager class which handles automatic enabling/disabling of controls
//     based on the CanExecute delegate.
Benjol
  • 63,995
  • 54
  • 186
  • 268