How can I open menu (System.Windows.Controls.Menu) programmatically in WPF?
5 Answers
Check out this example on how to open a context menu.
http://www.uxpassion.com/2009/01/how-to-enable-and-show-context-menu-on-left-click-in-wpf/
In summary
You can just call:
YourContextMenu.IsOpen = true; This will display the context menu, just make sure its associated with a FrameworkElement on which it is displaying)
3 Comments
Reg Edit
No, the OP is asking about the Menu class (System.Windows.Controls.Menu). It doesn't have an IsOpen property. Perhaps you are thinking of the ContextMenu class.
zmechanic
You will also need to initialize
YourContextMenu.PlacementTarget with intended "target" UIElement. Otherwise, the menu constructed dynamically from XAML string (using XamlReader) will not be able to resolve resources, and will have no icons, for example.mike
What also helps is listening to
ContextMenu.Opened and then calling CnotextMenu.Focus() to ensure that it receives keyboard input, e.g. to ensure the ESC key is handled by the context menu (to close it) and that arrow keys can be used for navigation.