Horizontal Menu with Horizontal SubMenu in WPF

断了今生、忘了曾经 提交于 2020-01-01 06:54:08

问题


I have been struggling with the WPF Menu and I simply cannot achieve what I want to.

I tried changing default styling on the Menu and MenuItem control, but this turned out terrible. I then tried styling it by making use of control templates, however, I think I came to realization that my WPF skill just aren't there yet.

Here is a picture of my desired result:

Is there anyone that can maybe point me in the right direction? Or maybe assist me with some xaml that will give my desired result?


回答1:


You can set ItemsPanel with horizontal orientation, like this:

<Menu.ItemsPanel>
    <ItemsPanelTemplate>
        <VirtualizingStackPanel Orientation="Horizontal" />
    </ItemsPanelTemplate>
</Menu.ItemsPanel>

Because both Menu and MenuItem are derived from ItemsControl.

Full example:

<Menu Width="100" 
        Height="30"
        Margin="10" 
        VerticalAlignment="Top"
        HorizontalAlignment="Left"
        Background="White"
        BorderBrush="Blue" 
        BorderThickness="1">

    <Menu.ItemsPanel>
        <ItemsPanelTemplate>
            <VirtualizingStackPanel Orientation="Horizontal" />
        </ItemsPanelTemplate>
    </Menu.ItemsPanel>

    <MenuItem Header="_ITEM1">
        <MenuItem.ItemsPanel>
            <ItemsPanelTemplate>
                <VirtualizingStackPanel Orientation="Horizontal"/>
            </ItemsPanelTemplate>
        </MenuItem.ItemsPanel>

        <MenuItem Header="_SUBMENU1" IsCheckable="true"/>
        <MenuItem Header="_SUBMENU2" IsCheckable="true"/>
        <MenuItem Header="_SUBMENU3" IsCheckable="true"/>

        <Separator/>
    </MenuItem>

    <MenuItem Header="_ITEM2">
        <MenuItem Header="Undo"/>
        <MenuItem Header="Redo"/>
        <Separator/>
        <MenuItem Header="Cut"/>
        <MenuItem Header="Copy"/>
        <MenuItem Header="Paste"/>
    </MenuItem>            
</Menu>

Output



来源:https://stackoverflow.com/questions/21813348/horizontal-menu-with-horizontal-submenu-in-wpf

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!