tags:

views:

53

answers:

2

I have a custom command and I try to execute them from the context menu, but they are always displayed as disabled unless I click any button on the UI (buttons do not have anything to do with commands).

After clicking a button, commands start to be displayed correctly (when they are unavailable they get disabled and enabled if available).

Edit: it turns out that it is not the button click which makes command work correctly, but button or other controls in focus (e.g. if I tab into a control this also enables my commands).

Here is the code for commands:

<Window.InputBindings>
    <KeyBinding Command="{x:Static local:MainWindow.Quit}" Key="Q" Modifiers="Ctrl"/>
    <KeyBinding Command="{x:Static local:MainWindow.Disconnect}" Key="D" Modifiers="Ctrl"/>
</Window.InputBindings>

<Window.ContextMenu>
    <ContextMenu Opacity="95">
        <MenuItem Header="Quit Application                  Ctrl + Q"   Command="{x:Static local:MainWindow.Quit}"/>
        <MenuItem Header="Disconnect from the pump   Ctrl + D" Command="{x:Static local:MainWindow.Disconnect}"/>
    </ContextMenu>
</Window.ContextMenu>

Here is the commands CanExecuteMethod:

public static RoutedCommand Quit = new RoutedCommand();   

private void QuitCanExecute(object sender, CanExecuteRoutedEventArgs e)
     {
      e.CanExecute = true;
      e.Handled = true;
     }
+1  A: 

There is probably be some change "behind the scenes" that would normally enable the commands, but the view is not aware of this change. One would need to see the Command-implementations to give more precise hints.

You can either make anything that changes your command-enable-state notify the view or manually trigger a command-refresh via CommandManager.InvalidateRequerySuggested(), for example when the context-menu opens.

WPF-ICommands work that way, that they requery their "CanExecute"-function whenever something in the view changes (e.g. PropertyChanged-event is fired or a button is clicked), but they don't requery if they have no reason to do that.

Simpzon
The problem is that one command's CanExecute method always returns true, but the command is still disabled, if no visual element are in focus.
Vitalij
I think it is some timing-problem, maybe your window (which seems to define the commands) is not ready when the commands are initialized for the first time.
Simpzon
But when you open the context menu it should run CanExecute method is it correct? However if I debug my application, first time CanExecute method is called is after I have tabed or clicked a control and then opend context menu. And from that point it start to call CanExecute method every time I right click, but before there are no calls to CanExecute method.
Vitalij
Your Commands are static properties, but are they also initialized statically and do they use only static dependencies?It needs your code to answer these questions, please post the code defining your commands.
Simpzon
+1  A: 

Completely different track, now: there is indeed something special about the ContextMenu as the carrier for commands: the menu is not regarded as part of the window and therefore does not behave like an element in its visual tree would.

There are different solutions for your problems defined here: http://www.wpftutorial.net/RoutedCommandsInContextMenu.html

The easiest approach seems to be adding this to your XAML (for the window):

FocusManager.FocusedElement="{Binding RelativeSource={x:Static RelativeSource.Self}, Mode=OneTime}"
Simpzon
Right, I completely forgot that context menu has a separate visual tree. Later I came to similar solution, but I couldn't understand why it works. I even had read this article some time ago. But thanks anyway, that's helpful!
Vitalij