WPF MenuItem.Icon 尺寸
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/15725729/
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 MenuItem.Icon dimensions
提问by armanali
So I have a MenuItem (within a ContextMenu, if it really makes a difference, don't think it does). I want to display an icon within the MenuItem using the MenuItem.Icon property. Here is XAML code that does this:
所以我有一个 MenuItem(在 ContextMenu 中,如果它真的有区别,不要认为它有区别)。我想使用 MenuItem.Icon 属性在 MenuItem 中显示一个图标。这是执行此操作的 XAML 代码:
<MenuItem Header="MenuItem1">
<MenuItem.Icon>
<Image Source="[pathtoimage]"/>
</MenuItem.Icon>
</MenuItem>
This works. But what if I want to specify the size of the icon with Height and Width ? Search on the web reveals that it should be 32, some say it should be 20. 20 works the best for me, but I don't want to hardcode 20. I want to set it to the same height as the MenuItem, given the fact that on a different resolution the actual height might be different. So, I tried the following:
这有效。但是如果我想用 Height 和 Width 指定图标的大小怎么办?在网上搜索显示它应该是 32,有人说它应该是 20。20 对我来说效果最好,但我不想硬编码 20。我想将它设置为与 MenuItem 相同的高度,因为事实上,在不同的分辨率下,实际高度可能会有所不同。所以,我尝试了以下方法:
<MenuItem x:Name="menuItem1" Header="MenuItem1">
<MenuItem.Icon>
<Image Source="[pathtoimage]"
Height="{Binding Path=ActualHeight, ElementName=menuItem1}"
Width="{Binding Path=ActualHeight, ElementName=menuItem1}"
</MenuItem.Icon>
</MenuItem>
You would think that it should work, but it didn't. The question is why ? I am able to get my desired appearance if I set both Width and Height to 20. But if the system font size gets changed, then 20x20 won't be ideal anymore. It would be ideal to do this in the XAML, because I am sure there are a lot of ways of handling this in the code-behind.
你会认为它应该起作用,但它没有。问题是为什么?如果我将 Width 和 Height 都设置为 20,我就能得到我想要的外观。但是如果系统字体大小改变了,那么 20x20 就不再是理想的了。在 XAML 中执行此操作是理想的,因为我确信在代码隐藏中有很多方法可以处理此问题。
采纳答案by Ilya Gerasimets
Icon size and icon area width are hardcoded inside the MenuItem templates and styles like the following.
图标大小和图标区域宽度硬编码在 MenuItem 模板和样式中,如下所示。
<ContentPresenter x:Name="Icon" Content="{TemplateBinding Icon}" ContentSource="Icon" HorizontalAlignment="Center" Height="16" Margin="3" SnapsToDevicePixels="{TemplateBinding SnapsToDevicePixels}" VerticalAlignment="Center" Width="16"/>
So if you want dynamic icon size you need to copy all the templates and styles related to MenuItem from one of the SystemThemes (e.g. aero2.normalcolor.xaml) and then you can customize them for your needs. In your case it would need vast amount of changes in those templates and styles. Unfortunately there are no simpler ways to do this. And it is not scalable either since you would stuck with just one System Theme unless you change all of them.
因此,如果您想要动态图标大小,您需要从 SystemThemes 之一(例如 aero2.normalcolor.xaml)复制与 MenuItem 相关的所有模板和样式,然后您可以根据需要自定义它们。在您的情况下,需要对这些模板和样式进行大量更改。不幸的是,没有更简单的方法可以做到这一点。而且它也无法扩展,因为除非您更改所有系统主题,否则您只会使用一个系统主题。
Similar things were suggested in the related topics here: WPF - How to style the menu control to remove the left margin?or System.Windows.Controls.MenuItem without icon area.
此处的相关主题中提出了类似的建议:WPF - How to style the menu control to remove the left margin? 或System.Windows.Controls.MenuItem 没有图标区域。
回答by sa_ddam213
Well basically the problem is due to the MenuItem Header, the Header is of type objectand you add a string so it has no Height/Width, so the MenuItemActualHeightwill return Auto
那么基本上问题是由于 MenuItem Header, Header 是类型,object并且您添加了一个字符串,因此它没有高度/宽度,因此MenuItemActualHeight将返回Auto
If you were to add a Textblockas the Header this will give the MenuItema Height that can be used to set your image size.
如果您要添加 aTextblock作为标题,这将提供MenuItem可用于设置图像大小的高度。
<MenuItem x:Name="menuItem1" >
<MenuItem.Header>
<TextBlock Text="MenuItem1" />
</MenuItem.Header>
<MenuItem.Icon>
<Image Height="{Binding Header.ActualHeight, ElementName=menuItem1}"
Width="{Binding Header.ActualHeight, ElementName=menuItem1}" />
</MenuItem.Icon>
</MenuItem>
So with this in mind you could easily make a ItemContainerTemplatefor the MenuItemso the all behave in this fashion
因此,考虑到这一点,您可以轻松地制作一个ItemContainerTemplateforMenuItem以便所有人都以这种方式行事

