【问题标题】:Workaround to WPF Menu Item Icon Behaviour in Windows 8Windows 8 中 WPF 菜单项图标行为的解决方法
【发布时间】:2013-02-06 11:19:32
【问题描述】:

我的 WPF 应用程序中有一个菜单,它使用 PNG 图像作为图标。这些图像作为资源存储在单独的 DLL 中。在旧版本的 Windows 中,图标(48x48 像素)正确显示,缩小到容器的大小。

在 Windows 8 中,图标的行为有所不同。

这是我的代码示例:

<MenuItem Header="Promotions" x:Name="mnuPromotions" Style="{StaticResource styMenuItem}">
    <MenuItem.Icon>
        <Image Source="{StaticResource icnPromotion_48}" Style="{StaticResource styMenuIcon}" />
    </MenuItem.Icon>
</MenuItem>
<MenuItem Header="Catalogues" x:Name="mnuCatalogues" Style="{StaticResource styMenuItem}">
    <MenuItem.Icon>
        <Image Source="{StaticResource icnCatalogue_48}" Style="{StaticResource styMenuIcon}" />
    </MenuItem.Icon>
</MenuItem>
  <MenuItem Header="Customer Price Lists" x:Name="mnuCustomerPriceLists">
    <MenuItem.Icon>
        <Image Source="{StaticResource icnCashCustomer_48}" Style="{StaticResource styMenuIcon}" />
    </MenuItem.Icon>
</MenuItem>

这里是菜单项的样式

<Style x:Key="styMenuItem" TargetType="MenuItem">
  <Setter Property="Foreground" Value="#FF000000" />
  <Setter Property="FontSize" Value="22" />
  <Setter Property="Margin" Value="2" />
  <Setter Property="Background">
    <Setter.Value>
      <LinearGradientBrush EndPoint="0.5,1" StartPoint="0.5,0">
        <GradientStop Offset="0" Color="#AA9D9D9D" />
        <GradientStop Offset="1" Color="#AACFCDBE" />
      </LinearGradientBrush>
    </Setter.Value>
  </Setter>
</Style>

这是图标的样式

<Style x:Key="styMenuIcon" TargetType="Image">
  <Setter Property="Width" Value="20" />
  <Setter Property="Height" Value="20" />
  <Setter Property="VerticalAlignment" Value="Center" />
  <Setter Property="HorizontalAlignment" Value="Center" />
  <Setter Property="Margin" Value="2" />
</Style>

这是旧版本 Windows 中菜单的外观:

这就是菜单在 Windows 8 中的样子

如果您仔细观察,这些图标在 Win8 中居中并被裁剪,而在旧版本的 Windows 中它们适合容器。

有谁知道为什么这种行为会发生变化,是否有一个简单的解决方法来解决这种行为变化,以便菜单与从 XP 到 8 的所有 Windows 版本完全兼容?

【问题讨论】:

    标签: wpf windows-8


    【解决方案1】:

    看起来这种行为是一个已知的错误,(像往常一样)微软不想修复:

    http://connect.microsoft.com/VisualStudio/feedback/details/767328/menuitem-icon-wont-stretch-when-changing-size-in-windows-8

    发布了两种解决方法,一种简单的方法是将图像高度调整为每个 XAML 的菜单项高度:

    <MenuItem x:Name="ctxMenuName" Header="Open Existing">
      <MenuItem.Icon>
         <Image Source="/MyApp;component/Images/folder_with_file.png"
                     Height="{Binding Path=ActualHeight, ElementName=ctxMenuName}"
                     Width="{Binding Path=ActualHeight, ElementName=ctxMenuName}" />
      </MenuItem.Icon>
    </MenuItem> 
    

    【讨论】:

      【解决方案2】:

      在升级现有 WPF 应用程序以支持 Windows 10 时遇到这种情况。这是从 MenuItem 继承的解决方法。这将适用于其他内容以及图像。

      using System.Windows;
      using System.Windows.Controls;
      using System.Windows.Media;
      
      namespace MenuItemDemo
      {
          public class MenuItemEx : MenuItem
          {
              public MenuItemEx()
              {
                  base.Loaded += FrameworkElement_OnLoaded;
              }
      
              private void FrameworkElement_OnLoaded(object sender, RoutedEventArgs e)
              {
                  var child = Icon as FrameworkElement;
                  if (child == null)
                      return;
      
                  var parent = VisualTreeHelper.GetParent(child) as ContentPresenter;
                  if (parent == null)
                      return;
      
                  parent.Width = child.Width;
                  parent.Height = child.Height;
              }
          }
      }
      

      然后在xaml中引用xmlns:menuitem="clr-namespace:MenuItemDemo",然后将&lt;MenuItem&gt;改为&lt;menuitem:MenuItemEx&gt;

      一个使用这个的示例窗口

      <Window x:Class="MenuItemDemo.MainWindow"
              xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
              xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
              xmlns:menuitem="clr-namespace:MenuItemDemo"
              Title="MainWindow" Height="350" Width="525">    
              <DockPanel>
                  <Menu DockPanel.Dock="Top">
                      <menuitem:MenuItemEx>
                          <MenuItem.Icon>
                              <Label>Text asdfasdf</Label>
                          </MenuItem.Icon>
                      </menuitem:MenuItemEx>  
                  </Menu>
                  <Grid></Grid>
              </DockPanel>     
      </Window>
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2011-12-14
        • 2012-01-11
        • 2014-11-18
        • 2019-07-09
        相关资源
        最近更新 更多