I have hooked up a context menu item in a listbox item such that tapping it will change its state. I need either the menu to stay open after the item has been selected, or to programmatically reopen the menu right after it closes.
My menu looks like so:
Some Command 1
Some Command 2
Some Command 3
Inverted
And the user can tap the Inverted command and then tap one of the other commands to cause them to function in Inverted Mode, and the menu through data binding appears like so:
Some Command 1
Some Command 2
Some Command 3
Inverted ✔
Not being able to figure out how to keep the menu open after tap, I’ve tried the less ideal reopen menu approach like so:
private void onCommandInvert(object sender, RoutedEventArgs e)
{
CommandState.Instance.Inverted = !CommandState.Instance.Inverted;
// Open it again.
MenuItem menuItem = (MenuItem)sender;
ContextMenu menu = (ContextMenu)menuItem.Parent;
menu.IsOpen = true;
}
But doing so throws the following exception on the menu.IsOpen = true statement:
A first chance exception of type 'System.InvalidOperationException' occurred in
System.Windows.dll
An unhandled exception of type 'System.InvalidOperationException' occurred in
System.Windows.dll
Additional information: Element is already the child of another element.
I have also tried the following with the Closed event, with the same exception occurring:
private void onContextMenuClosed(object sender, RoutedEventArgs e)
{
ContextMenu menu = (ContextMenu)sender;
menu.IsOpen = true;
}
Any ideas? Thanks!
I got it! Thanks to willmel’s comment, I digged through the source code for the
MenuItemand was able to overrideOnClick()to do exactly what I needed (the ideal solution no doubt). I couldn’t accessClickhowever, so I needed to introduce aStayClickevent property as well.Enjoy!
and in the page’s xaml, instead of
toolkit:MenuItemyou usemy:MenuItemEx