我已将预设功能表控件加入至使用者控件。我需要设定功能表的样式,以移除包含图标或核取方块空间的左边界。我该如何做?
XAML文件:
<Menu>
<MenuItem Header="MyMenu" FontSize="10">
<MenuItem Header="Options..." />
<MenuItem Header="About" />
</MenuItem>
</Menu>
它目前的呈现方式与任何其他现成的Menu控件相同:
我不希望菜单项左边的边距或列。这通常用于图标等。
9条答案
按热度按时间bwntbbo31#
我认为这就是您所追求的(同样,使用Expression Blend来实现它,但就其显示内容而言,这是我所能得到的最简约的...并且它花了很多时间)...您可以将以下内容作为示例放在空白WPF应用程序中:
cgvd09ve2#
Simpal和Sort的方式如下:创建ItemsPanelTemplate资源
将下面的MenuItem样式添加到资源中,操作就完成了。
要将同一样式应用于ContextMenu,您需要再创建一个样式,如下所示-
也在Forest上下文菜单上方,您必须添加
所以它将覆盖图标空间和显示大小写文本块。这是最简单和最容易的解决方案。
toiithl63#
这不是很简单,但您需要创建一个MenuItemStyle,通过Expression Blend可以轻松实现:
它创建了一个非常冗长的模板和样式集,您需要编辑菜单项以删除网格的固定宽度第一列,然后在SubMenuBorder ContentControl模板中,删除形成背景阴影的矩形。我已经附加了一个删除了边距的示例项目。Download sample project here.
drkbr07n4#
这里有两个选项:
1.将
ItemsPanelTemplate
设置为MenuItem
或ContextMenu
,具体取决于您使用的菜单类型(请参阅details)。Menu
样式.有两种现成得样式:Menu
的XAML样式(ordinary Menu和ContextMenu)jgzswidk5#
我的简单方法是对ItemTemplate中的Grid使用负边距
请参阅完整答案here
a11xaf1n6#
我使用WPF Notifyicon(hardcodet)并使用以下代码删除了菜单的图标部分:
trnvg8h37#
要删除空格并且不再使用图标,您必须更改MenuItem.SubmenuItemTemplateKey的模板或MenuItem的模板。如果您只需要阅读竖线并继续使用图标空格,请遵循以下答案。
带有网格的Windows将我的CustomContextMenu.xaml作为网格资源:
这是我的CustomContextMenu.xaml,它有一个CustomSeparatorStyle模板,用于将分隔线扩展到上下文菜单的左边距,还有一个ContextMenu模板,用于隐藏垂直线。
右侧菜单是用上面的代码创建的。您可以注意到大小和阴影的不同。为了保留原始菜单的阴影,您必须排除边框。效果
5uzkadbs8#
使用RadMenuGroupItem。
RadMenuGroupItem继承自RadMenuItem类,它用作RadMenuItem下拉列表的容器。换句话说,任何UI元素都可以放置在RadMenuGroupItem中。默认情况下,RadMenuGroupItem的背景色为白色,并且没有与RadMenuItem不同颜色的图标区域。因此您可以轻松地在下拉列表中使用不同大小的图标。除此之外,RadMenuGroupItem还有一个Header属性,该属性显示在所有组项的顶部。
结果是:
2sbarzqh9#
感谢您的成功想法。对于.net框架4.5和VS 2012我写了相应的ContextMenu和MenuItem: