WPF - 如何设置菜单控件的样式以删除左边距?
我已将默认菜单控件添加到我的用户控件中。我需要设置菜单样式以删除包含图标或复选框空间的左边距。我该怎么做?
XAML:
<Menu>
<MenuItem Header="MyMenu" FontSize="10">
<MenuItem Header="Options..." />
<MenuItem Header="About" />
</MenuItem>
</Menu>
它目前像任何其他开箱即用的菜单控件一样呈现:
我不想要菜单项左侧的边距或栏。这通常用于图标等。
I have added a default menu control to my user control. I need to style the menu to remove the left margin containing the space for the icon or checkbox. How can I do this?
XAML:
<Menu>
<MenuItem Header="MyMenu" FontSize="10">
<MenuItem Header="Options..." />
<MenuItem Header="About" />
</MenuItem>
</Menu>
It currently renders like any other Menu control out of the box:
I don't want the margin or column to the left of the menu items. This is typically used for icons etc.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(9)
我认为这就是你所追求的(再次,使用 Expression Blend 来解决它,但就其显示的内容而言,它是我能得到的最简约的......并且花了很多时间来玩弄)......您可以将以下内容放入空白 WPF 应用程序中作为示例:
I think this is what you're after (again, got around to it using Expression Blend, but its the most minimalist I could get in terms of what it displays...and it took a lot of playing around with)...you can just drop the following in a blank WPF application as an example:
简单而简短的方法如下。创建
ItemsPanelTemplate
资源:将以下
MenuItem
样式添加到资源中,即可完成。要将相同的
Style
应用于ContextMenu
,您需要再创建一个Style
,如下所示:在上下文菜单的上方,您还必须添加:
所以它将覆盖图标空间并显示大小写文本块。这是最简单、最容易的解决方案。
Simple and short way is below. Create an
ItemsPanelTemplate
resource:Add below
MenuItem
style to resources and you are done.To apply same
Style
to aContextMenu
, you need to create one moreStyle
as following:Also above of that for context menu you have to add:
So it will override icon space and show case textblock. It is the simple and most easy solution.
这不是很直接,但您需要创建一个 MenuItemStyle,最简单的是通过 Expression Blend:
它创建一组极其详细的模板和样式,您需要编辑菜单项以删除网格的固定宽度第一列,然后在 SubMenuBorder ContentControl 模板中,删除形成背景阴影的矩形。我附上了一个已删除边距的示例项目。
在此处下载示例项目。
It's not very straight forward, but you need to create a MenuItemStyle, easiest through Expression Blend:
It creates an extremely verbose set of templates and styles, and you need to edit the menu item to remove the fixed width first column of the grid, then in the SubMenuBorder ContentControl template, remove the rectangles which form the background shading. I've attached a sample project with margins removed.
Download sample project here.
这里有两个选项:
简短、简单、直接。将
ItemsPanelTemplate
设置为MenuItem
或ContextMenu
,具体取决于您使用的菜单类型(请参阅 详细信息)。激进。从头开始重写
Menu
样式。有两种现成的样式:MahApps.Metro 的- XAML 风格的
- Jeff Wilcox 的风格启发了 MahApps 的前一个风格 (链接)
Menu
(普通菜单 和 ContextMenu)Two options here:
Short, simple and straight forward. Set
ItemsPanelTemplate
forMenuItem
orContextMenu
, depending what kind of menu you are using (see details).Radical. Rewrite
Menu
style from scratch. There are two ready to use styles:Menu
from MahApps.Metro (ordinary Menu and ContextMenu)我的简单方法是在 ItemTemplate 中为网格使用负边距,
请参阅此处的完整答案
My simple way is to use a negative margin for the Grid in the ItemTemplate
see full answer here
我正在使用 WPF Notifyicon (硬编码)并使用以下代码删除了菜单的图标部分:
I was working with WPF Notifyicon (hardcodet) and removed the icon section of the menu with this code:
要删除空格并且从不使用图标,您必须更改
MenuItem.SubmenuItemTemplateKey
的模板或MenuItem
的模板。如果您只需要摆脱垂直线并继续使用图标空间,请遵循此答案。
带有网格的 Windows 将我的
CustomContextMenu.xaml
作为网格资源:这是我的
CustomContextMenu.xaml
,它有一个CustomSeparatorStyle
模板来将分隔线扩展到上下文菜单的左边距。以及一个隐藏垂直线的 ContextMenu 模板。右侧菜单是使用上面的代码创建的。您可以注意到大小和阴影的差异。为了保留原始菜单的阴影,您必须排除
Border.Effect
。To remove the space and never use icons you have to change the template of
MenuItem.SubmenuItemTemplateKey
or the template ofMenuItem
.If you just need to get rid of the vertical line and keep using the icons space follow this answer.
Windows with grid has my
CustomContextMenu.xaml
as grid resource:Here is my
CustomContextMenu.xaml
that has aCustomSeparatorStyle
template to extend the separation line to the left margin of the context menu. And a ContextMenu template to hide the vertical line.The right side menu is created with the code above. You can notice the difference in size and shadow. In order to keep the shadow of the original menu you have to exclude the
Border.Effect
.使用 RadMenuGroupItem。
这就是结果:
Use a RadMenuGroupItem.
And this is the result:
感谢您的成功想法。对于 .net Framework 4.5 和 VS 2012,我相应地为 ContextMenu 和 MenuItem 编写了:
Thanks for succesfull idea. For .net Framework 4.5 and VS 2012 I wrote for ContextMenu and MenuItem accordingly: