none
CommandManager.InvalidateRequerySuggested() is not consistent in calling CommandCanExecute event.

    Question

  •  

    I have Print Menu and it should be anabled whenever Active Page is not null. But after loading al the pages when i say "CommandManager.InvalidateRequerySuggested()" , it does not call CommandCanExecute event everytime. It shows inconsistent behaviour. Please help me out to call CommandCanExecute consistently.
    Monday, May 12, 2008 9:06 AM

Answers

  • CanExecute will be fired up both explicitly and implicitly, but it will be fired up typically when the specified InputBinding(MouseBinding, KeyBinding) is triggered, or a specified ICommandSource implementation such as Button has been clicked or MenuItem has been clicked etc. And you can also explicitly fire up CanExecute by calling CommandManager.InvalidateRequerySuggested(), by explicitly find a specific command, and call its CanExecute() method.

    But knowing all of those implementation detail stuff doesn't help you solve your issue, right? you'd better provide a small, complete and ready-to-run example to demonstrate how InvalidateRequerySuggested is inconsistent for your scenario?

    Thanks
    Tuesday, May 13, 2008 9:19 AM
  • Issue resolved when i used -

     

    Application.Current.Dispatcher.Invoke(

    DispatcherPriority.Normal,

    (DispatcherOperationCallback)delegate(object arg)

    {

    CommandManager.InvalidateRequerySuggested();

    return null;

    },

    null);

     

     

     

    instead of

     

    mainwindow.Dispatcher.Invoke(

    DispatcherPriority.Normal,

    (DispatcherOperationCallback)delegate(object arg)

    {

    CommandManager.InvalidateRequerySuggested();

    return null;

    },

    null);

    Monday, May 19, 2008 6:16 AM

All replies

  • Could you please provide a small, complete and ready-to-run example to demonstrate the issue you are encountering?


    Thanks

    Tuesday, May 13, 2008 5:14 AM
  • Hi,

    Will you please let me know when exactly CanExecute Event gets fired? Whats the condition for CanExecute to be fired?

     

    Regards,

    Harshal

    Tuesday, May 13, 2008 8:56 AM
  • CanExecute will be fired up both explicitly and implicitly, but it will be fired up typically when the specified InputBinding(MouseBinding, KeyBinding) is triggered, or a specified ICommandSource implementation such as Button has been clicked or MenuItem has been clicked etc. And you can also explicitly fire up CanExecute by calling CommandManager.InvalidateRequerySuggested(), by explicitly find a specific command, and call its CanExecute() method.

    But knowing all of those implementation detail stuff doesn't help you solve your issue, right? you'd better provide a small, complete and ready-to-run example to demonstrate how InvalidateRequerySuggested is inconsistent for your scenario?

    Thanks
    Tuesday, May 13, 2008 9:19 AM
  • Thanks for the reply. Creating a new application for reproducing same scenario will be bit difficult and time consuming task. Can you please tell me in following scenario what action will cause CanExecute to be fired for 'PrintActivePage' Menu -

     

    <sr:MenuItem Text="_Add" Command="{x:Static shell:Commands.Add}"

    CommandTarget="{Binding Source={x:Static Application.Current}, Path=MainWindow}" />

    <sr:MenuItem Text="_Add Fixed"

    Command="{x:Static shell:Commands.AddFixed}"

    CommandTarget="{Binding Source={x:Static Application.Current}, Path=MainWindow}" />

    <sr:MenuItem Text="_Import" Activate="OnImport" IsEnabled="False"

    Name="ImportMenuItem" />

    <sr:MenuItem Text="_Reset"

    Command="{x:Static shell:Commands.Reset}"

    CommandTarget="{Binding Source={x:Static Application.Current}, Path=MainWindow}" />

    <sr:MenuItem Text="_Save"

    Command="{x:Static shell:Commands.Save}"

    CommandTarget="{Binding Source={x:Static Application.Current}, Path=MainWindow}" />

    <sr:MenuItem Text="_Print" IsSplit="True">

    <sr:RibbonPopup>

    <sr:Heading>Preview and print</sr:Heading>

    <sr:Menu>

    <sr:MenuItem Text="_Print" Command="shell:Commands.PrintActivePage"/>

    <sr:MenuItem Text="Print Active" Command="shell:Commands.PrintActive"/>

    </sr:Menu>

    </sr:RibbonPopup>

    </sr:MenuItem>

    <Separator />

    <sr:MenuItem Text="_Close" />

    </sr:ApplicationPopup>

     

     

     

     

    Tuesday, May 13, 2008 11:23 AM
  • When the child MenuItems will be popped up, aka the parent MenuItem.IsSubmenuOpen is set to true, the CanExecute will be called, is this information helpful to your issue at hand?

    Please use the reflector to examine the implementation of MenuItem.OnIsSubmenuOpenChanged method implementation to have a better understanding of what's going on.

    Thanks
    Wednesday, May 14, 2008 4:40 AM
  • Issue resolved when i used -

     

    Application.Current.Dispatcher.Invoke(

    DispatcherPriority.Normal,

    (DispatcherOperationCallback)delegate(object arg)

    {

    CommandManager.InvalidateRequerySuggested();

    return null;

    },

    null);

     

     

     

    instead of

     

    mainwindow.Dispatcher.Invoke(

    DispatcherPriority.Normal,

    (DispatcherOperationCallback)delegate(object arg)

    {

    CommandManager.InvalidateRequerySuggested();

    return null;

    },

    null);

    Monday, May 19, 2008 6:16 AM
  • Please let me know what could be the difference between Application.Current.Dispatcher and normal Dispatcher. Also how can we check whether particular code is running on main thread or not after using Dispatcher?

     

    Monday, May 19, 2008 7:34 AM
  • Harsh007 - do you mean like this?

                    
     
     (Application.Current.Dispatcher.Thread == Thread.CurrentThread)

    Cheers,
    James Miles
    • Edited by James Miles Thursday, January 22, 2009 5:38 AM stupid code
    Friday, November 21, 2008 5:24 AM