在 WPF 中以编程方式显示菜单
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1165562/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Show menu programmatically in WPF
提问by Dmitry
How can I open menu (System.Windows.Controls.Menu) programmatically in WPF?
如何在 WPF 中以编程方式打开菜单 (System.Windows.Controls.Menu)?
回答by Andreas Grech
Get hold of the menu item, and do this :
获取菜单项,然后执行以下操作:
_menuItem.IsSubmenuOpen = true;
回答by Dmitry
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/
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)
这将显示上下文菜单,只需确保它与显示它的 FrameworkElement 相关联)
回答by Joe Sonderegger
private void button_Click(object sender, RoutedEventArgs e)
{
var button= sender as FrameworkElement;
if (button != null)
{
button.ContextMenu.IsOpen = true;
}
}
回答by pablosan
void CmsBox_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
{
box = sender as WpfBox;
ContextMenu cms = new ContextMenu();
e.Handled = true;
...
}