带图像的 WPF 菜单项
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1763763/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
WPF menu item with image
提问by Vytas
How to define MenuItem.Icon so that the MenuItemHeader text would be placed below the menu item image?Thanks for help!
如何定义 MenuItem.Icon 以便将 MenuItemHeader 文本放置在菜单项图像下方?感谢您的帮助!
采纳答案by Ray Burns
The easy way way is to not use the Icon property but to instead put the icon in the Header:
简单的方法是不使用 Icon 属性,而是将图标放在标题中:
<Menu>
<MenuItem>
<MenuItem.Header>
<StackPanel>
<Image Width="20" Height="20" Source="/XSoftArt.WPFengine;component/Images/export32x32xp.png" />
<ContentPresenter Content="Reports" />
</StackPanel>
</MenuItem.Header>
</MenuItem>
<MenuItem Header="Export" />
<MenuItem Header="New record" />
</Menu>
For this simple case the <ContentPresenter Content="Reports" />
can be replaced with a <TextBlock Text="Reports" />
because that's what ContentPresenter would use to present the string anyway. For more complex Header=
, you could use the ContentPresenter
as shown.
对于这个简单的情况,<ContentPresenter Content="Reports" />
可以用 a 替换,<TextBlock Text="Reports" />
因为无论如何 ContentPresenter 都会使用它来呈现字符串。对于更复杂的Header=
,您可以使用ContentPresenter
如图所示的。
回答by DanielE
How something along the lines of:
如何沿着以下路线:
<ContextMenu>
<MenuItem Header="Reports">
<MenuItem.Icon>
<Image Source="/XSoftArt.WPFengine;component/Images/export32x32xp.png"/>
</MenuItem.Icon>
</MenuItem>
</ContextMenu>
回答by tridy
In the case of StackPaneluse Label and not the TextBlocksince only Labelwill allow you to have the mnemonics on the menu, like _Reports.
在StackPanel的情况下,使用 Label 而不是TextBlock,因为只有Label才会允许您在菜单上使用助记符,例如_Reports。