在WPF中以编程方式显示菜单

时间:2009-07-22 14:13:08

标签: wpf menu

如何在WPF中以编程方式打开菜单(System.Windows.Controls.Menu)?

5 个答案:

答案 0 :(得分:20)

抓住菜单项,然后执行以下操作:

_menuItem.IsSubmenuOpen = true;

答案 1 :(得分:20)

查看此示例,了解如何打开上下文菜单。

http://www.uxpassion.com/2009/01/how-to-enable-and-show-context-menu-on-left-click-in-wpf/

总结

您可以致电:

YourContextMenu.IsOpen = true;

这将显示上下文菜单,只需确保它与显示它的FrameworkElement相关联。

答案 2 :(得分:3)

private void button_Click(object sender, RoutedEventArgs e)
{
    var button= sender as FrameworkElement;
    if (button != null)
    {
        button.ContextMenu.IsOpen = true;
    }
}

答案 3 :(得分:0)

void CmsBox_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
{
    box = sender as WpfBox;
    ContextMenu cms = new ContextMenu();
    e.Handled = true;
    ...
}

答案 4 :(得分:0)

        private void MainGrid_Loaded(object sender, RoutedEventArgs e)
        {
            IncList.ItemsSource = m_DataSource;
            IncList.ContextMenu = new ContextMenu();
            IncList.ContextMenu.Items.Add(new MenuItem() { Header = "Test1" });
            IncList.ContextMenu.Items.Add(new MenuItem() { Header = "Test2" });
        }