2

I would like to use the LeftAlt and RightAlt keys to alter MenuItems within an open ContextMenu. I want this all happen while the menu is already open - not while right mouse is clicked to open the context menu. I did the following:

ContextMenu.KeyDown += ContextMenu_KeyDown;

void ContextMenu_KeyDown(object sender, KeyEventArgs e)
    {
        if( e.Key == Key.LeftAlt || e.Key == Key.RightAlt )
        {
            e.Handled = true;
            // DEMO
            MenuItem firstItem = this.ContextMenu.Items[0] as MenuItem;
            if( firstItem != null ) firstItem.Header = "Alt Pressed!";
        }
    }

Unfortunately this is not working. As soon as I press the Alt-Key the ContextMenu is closed although i use e.Handled = true;. Why is this? How can I catch the Alt-Keys and alter the context menu and leave the menu open?

zpete
  • 1,725
  • 2
  • 18
  • 31

1 Answers1

0

According to this post (WPF: When Alt key is pressed, my ContextMenu won't open), it is a built-in behavior of the MenuBase class. You will need to choose another modifier key to accomplish this.

Here is the MSDN page explaining it : https://msdn.microsoft.com/en-us/library/system.windows.controls.primitives.menubase.onkeydown.aspx

Community
  • 1
  • 1
mgarant
  • 565
  • 5
  • 18