WPF中带有水平子菜单的水平菜单 [英] Horizontal Menu with Horizontal SubMenu in WPF
问题描述
我一直在为WPF Menu
苦苦挣扎,而我根本无法实现我想要的目标.
I have been struggling with the WPF Menu
and I simply cannot achieve what I want to.
我尝试更改Menu
和MenuItem
控件上的默认样式,但是结果很糟糕.然后我尝试通过使用控件模板来设置样式,但是,我想我意识到我的WPF技能还不存在.
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:
有人可以指出正确的方向吗?还是可以帮我一些xaml来达到我想要的结果?
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?
推荐答案
您可以将ItemsPanel
设置为水平方向,例如:
You can set ItemsPanel
with horizontal orientation, like this:
<Menu.ItemsPanel>
<ItemsPanelTemplate>
<VirtualizingStackPanel Orientation="Horizontal" />
</ItemsPanelTemplate>
</Menu.ItemsPanel>
因为Menu
和MenuItem
都是从ItemsControl派生的.
Because both Menu
and MenuItem
are derived from ItemsControl.
完整示例:
<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
Output
这篇关于WPF中带有水平子菜单的水平菜单的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!