如何在wpf中的另一个MenuItem中使用MenuItem

时间:2022-08-26 00:10:20

I want to make a menu like this in wpf

我想在wpf中制作这样的菜单

如何在wpf中的另一个MenuItem中使用MenuItem

I wrote this code:

我写了这段代码:

<MenuItem Header="Menu 4" MouseEnter="mousecom" Background="DarkGreen">
       <MenuItem.ContextMenu>
             <ContextMenu>
                   <MenuItem Header="submenu 1"/>
                   <MenuItem Header="submenu 2"/>                   
             </ContextMenu>
       </MenuItem.ContextMenu>
 </MenuItem>

and mousecom is:

和mousecom是:

private void mousecom(object sender, MouseEventArgs e)
        {
            while (IsMouseOver)
            {
                (sender as Button).ContextMenu.IsEnabled = true;
                (sender as Button).ContextMenu.PlacementTarget = (sender as Button);
                (sender as Button).ContextMenu.Placement = System.Windows.Controls.Primitives.PlacementMode.Right;
                (sender as Button).ContextMenu.IsOpen = true;
            }           
        }

I mean I wanna have a MenuItem and by coming mouse on it another menu opens including some other MenuItems.

我的意思是我想要一个MenuItem,并通过鼠标在其上打开另一个菜单,包括一些其他MenuItems。

Why it doesn't work?

为什么它不起作用?

how can I do that? (please pay attention to the picture)

我怎样才能做到这一点? (请注意图片)

1 个解决方案

#1


1  

You are using the MenuItem control incorrectly.

您正在错误地使用MenuItem控件。

To create submenus, you would do something like the following:

要创建子菜单,您可以执行以下操作:

<!-- Top level menu -->
<MenuItem>

    <!-- Level 1 menu -->
    <MenuItem>

        <!-- Level 2 menus -->
        <MenuItem />
        <MenuItem />
        <MenuItem />
    </MenuItem>

    <!-- More level 1 menus -->
    <MenuItem />
    <MenuItem />
    <MenuItem />
</MenuItem>

#1


1  

You are using the MenuItem control incorrectly.

您正在错误地使用MenuItem控件。

To create submenus, you would do something like the following:

要创建子菜单,您可以执行以下操作:

<!-- Top level menu -->
<MenuItem>

    <!-- Level 1 menu -->
    <MenuItem>

        <!-- Level 2 menus -->
        <MenuItem />
        <MenuItem />
        <MenuItem />
    </MenuItem>

    <!-- More level 1 menus -->
    <MenuItem />
    <MenuItem />
    <MenuItem />
</MenuItem>