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?