This topic has not yet been rated - Rate this topic

CommandManager.InvalidateRequerySuggested Method

Forces the CommandManager to raise the RequerySuggested event.

Namespace:  System.Windows.Input
Assembly:  PresentationCore (in PresentationCore.dll)
public static void InvalidateRequerySuggested()

The CommandManager only pays attention to certain conditions in determining when the command target has changed, such as change in keyboard focus. In situations where the CommandManager does not sufficiently determine a change in conditions that cause a command to not be able to execute, InvalidateRequerySuggested can be called to force the CommandManager to raise the RequerySuggested event.

The following example uses a DispatcherTimer to periodically call InvalidateRequerySuggested to force the CommandManager to raise the RequerySuggested event.


//  System.Windows.Threading.DispatcherTimer.Tick handler
//
//  Updates the current seconds display and calls
//  InvalidateRequerySuggested on the CommandManager to force 
//  the Command to raise the CanExecuteChanged event.
private void dispatcherTimer_Tick(object sender, EventArgs e)
{
    // Updating the Label which displays the current second
    lblSeconds.Content = DateTime.Now.Second;

    // Forcing the CommandManager to raise the RequerySuggested event
    CommandManager.InvalidateRequerySuggested();
}


.NET Framework

Supported in: 4, 3.5, 3.0

.NET Framework Client Profile

Supported in: 4, 3.5 SP1

Windows 7, Windows Vista SP1 or later, Windows XP SP3, Windows Server 2008 (Server Core not supported), Windows Server 2008 R2 (Server Core supported with SP1 or later), Windows Server 2003 SP2

The .NET Framework does not support all versions of every platform. For a list of the supported versions, see .NET Framework System Requirements.
Did you find this helpful?
(1500 characters remaining)
Community Content Add
Annotations FAQ
Use this with the MVVM object RelayCommand
When using the RelayCommand class, a command class used for MVVM development, you may find times when a button that is enabled or disabled based on RelayCommand.CanExecute() does not refresh until a keyboard or mouse click.  When this happens you may have to manually force this refresh to occur. You can force the refresh in the ViewModel with this function...

http://www.wpfsharp.com/2011/07/refreshing-a-button-enableddisabled-by-relaycommand-canexecute/
Documentation should indicate calling from the main UI thread.

It should be noted, that if you are using async calls, then the CommandManager.InvalidateRequerySuggested() should be called from the main thread, as any command listeners will be on the UI thread.

(Use a Dispatcher and call CheckAccess())