marcel
marcel

Reputation: 3272

Open Context Menu by ctrl+menu

I'd like to open the following context menu exclusively by pressing ctrl + menu:

<Window.ContextMenu>
    <ContextMenu>
        <MenuItem Name="item0" Header="mode_0"/>
    </ContextMenu>
</Window.ContextMenu>

I have another context menu which is bound to a button in the window. I want to be able to open the window-context menu whatever element is focused. To prevent that the window-context can't be opened at any time, e.g. the button is focused.

I tried to open it by:

Context.Menu.name.isOpened = true;

after checking the pressedKey-event, but the contextMenu closes right after releasing the menu button. Does anyone know a better .. and working way?

Upvotes: 1

Views: 661

Answers (3)

marcel
marcel

Reputation: 3272

Argh, the menu button triggers on the keyUp-event ... I've been listening to keyDown-event. That's why the menu opens on the key down and getting closed by releasing the menu button.

Upvotes: 0

SvenG
SvenG

Reputation: 5195

I think you will have to disable the Menu key in your window, otherwise the OS will try to resolve it ..

Something like that:

    public MainWindow()
    {
      InitializeComponent();
      this.PreviewKeyUp += MainWindow_PreviewKeyUp;

    }

    void MainWindow_PreviewKeyUp(object sender, KeyEventArgs e)
    {
      if (e.Key == Key.Apps)
      {
        e.Handled = true;
      }
    }

And afterwards open your own Context Menu programmatically.

Upvotes: 2

makc
makc

Reputation: 2579

Not sure If I understand what you are looking for correctly.
you probably should do something like that:

define a Command

 OpenMenu = new RoutedUICommand("OpenMenu ", "OpenMenu ", typeof(Commands), new   InputGestureCollection { 
                new KeyGesture(Key.O, ModifierKeys.Control, "Ctrl+O") });

use it in your Window

    <Window.CommandBindings>
        <CommandBinding Command="local:Commands.OpenMenu "
                        Executed="OpenMenuExecuted" />
   </Window.CommandBindings>

    private void OpenMenuExecuted(object sender, ExecutedRoutedEventArgs e)
    {
           this.ContextMenu.IsOpen = true;          
    }

note: if you dont want the right click button opening the window ContextMenu you can prevent it by attaching ContextMenuOpening event handler and setting the e.Handled = true;

Upvotes: 1

Related Questions