WPF MenuItem.Icon维度 [英] WPF MenuItem.Icon dimensions

查看:617
本文介绍了WPF MenuItem.Icon维度的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所以我有一个MenuItem(在ContextMenu中,如果它真的有所作为,不要认为它确实如此)。我想使用MenuItem.Icon属性在MenuItem中显示一个图标。以下是执行此操作的XAML代码:

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 Header="MenuItem1">
    <MenuItem.Icon>
        <Image Source="[pathtoimage]"/>
     </MenuItem.Icon>
</MenuItem>

这是有效的。但是如果我想用高度和宽度指定图标的大小呢?在网上搜索显示它应该是32,有人说它应该是20. 20对我来说效果最好,但我不想硬编码20.我想将它设置为与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:

<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>

您会认为它应该有效,但事实并非如此。问题是为什么?如果我将宽度和高度都设置为20,我就可以获得所需的外观。但是如果系统字体大小发生变化,那么20x20将不再理想。在XAML中这样做是理想的,因为我确信在代码隐藏中有很多方法可以解决这个问题。

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.

推荐答案

图标大小和图标区域宽度在MenuItem模板和类型中硬编码如下。

Icon size and icon area width are hardcoded inside the MenuItem templates and styles like the following.

            <ContentPresenter x:Name="Icon" Content="{TemplateBinding Icon}" ContentSource="Icon" HorizontalAlignment="Center" Height="16" Margin="3" SnapsToDevicePixels="{TemplateBinding SnapsToDevicePixels}" VerticalAlignment="Center" Width="16"/>

因此,如果你想要动态图标大小,你需要从一个复制所有与MenuItem相关的模板和样式系统主题(例如aero2.normalcolor.xaml)然后您可以根据需要自定义它们。在您的情况下,这些模板和样式需要进行大量更改。不幸的是,没有更简单的方法可以做到这一点。它不可扩展,因为除非你改变所有系统主题,否则你只会坚持使用一个系统主题。

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.

这里的相关主题提出了类似的建议: WPF - 如何设置菜单控件的样式以删除左边距? System.Windows.Controls.MenuItem without图标区域

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 MenuItem.Icon维度的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆