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

7

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?

Subalternate answered 17/5, 2011 at 9:4 Comment(2)
Currently, there's an implementation of RelayCommand in an additional namespace: GalaSoft.MvvmLight.CommandWpf that specifically solves this issue!Ninebark
I tried all the answers below, nothing worked. But using GalaSoft.MvvmLight.CommandWpf.RelayCommand instead solved it for me. Thank you @NinebarkTerefah
E
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;     
}
Endorsement answered 17/5, 2011 at 15:52 Comment(0)
P
45

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

Potentiate answered 5/4, 2013 at 8:5 Comment(8)
This is what I needed to doReciprocation
+1 for the hint on the UI thread. I called it on a background thread and nothing happenedManganate
With I could vote this up again. I wonder why this isn't done by default inside MVVMLightDrawer
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?Amphitrite
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.Potentiate
I tried a bunch of other options/suggestions. This was the first one to work for me. yay.Unaware
@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.Unaware
There is no document mentions that it should be invoked in UI thread, Microsoft!!Daisydaitzman
P
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.

Pluralize answered 17/5, 2011 at 10:45 Comment(2)
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.Mckeehan
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)Ninebark
S
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);
    //            }
}
Subalternate answered 19/5, 2011 at 15:42 Comment(1)
Currently, there's an implementation of RelayCommand in another namespace: GalaSoft.MvvmLight.CommandWpf that specifically solves this issue!Ninebark
W
0

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

Wolver answered 17/5, 2011 at 14:9 Comment(1)
This creates a compile error: 'The event System.Windows.Input.ICommand.CanExecuteChanged can only appear on the left hand side of += or -='Subalternate
E
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;     
}
Endorsement answered 17/5, 2011 at 15:52 Comment(0)
C
0

Just call Revert.RaiseCanExecuteChanged();

Captious answered 17/2, 2015 at 23:44 Comment(0)
D
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.
Drawer answered 1/2, 2021 at 8:13 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.