Only the last MenuItem gets the Icon

2019-07-26 08:02发布

问题:

I know there are other threads about this but in my case its a bit different.

I like to use an icon from a separate resource assembly

    <MultiTrigger>
      <MultiTrigger.Conditions>
      <Condition Property="IsCheckable"
                 Value="true" />
      <Condition Property="IsChecked"
                 Value="true" />
      <Condition Property="Role"
                 Value="SubmenuItem" />
      </MultiTrigger.Conditions>
      <Setter Property="Icon">
        <Setter.Value>
          <Image Margin="1,0"
                 Width="16"
                 Source="pack://application:,,,/MyResourceAssembly;
                         component/Resources/Connect_24.png"/>
        </Setter.Value>
      </Setter>
    </MultiTrigger>

This is used inside

<Style TargetType="{x:Type MenuItem}">

I tried the x:Share too but this didn't work cause of ResourceDictionary in a ResourceDictionary.

    <ResourceDictionary.MergedDictionaries>
      <ResourceDictionary Source="pack://application:,,,/MyResourceAssembly;component/Resources.xaml" />

      <ResourceDictionary>
        <Image x:Key="ConnectedIcon"
               x:Shared="false"
               Margin="1,0"
               Width="16"
               Source="pack://application:,,,/MyResourceAssembly;component/Resources/Connect_24.png"/>
      </ResourceDictionary>
    </ResourceDictionary.MergedDictionaries>

Do anybody has an idea to solve this problem. Adding the icon to any entry separately didn't solve the issue for me, cause in my application are about 200 items.

Best Regards

回答1:

The resource at the right place solves the problem.

<ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
                    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
    <Image x:Key="ConnectedIcon"
           x:Shared="False"
           Source="pack://application:,,,/MyResourceAssembly;component/Resources/Connect_24.png"
           Margin="1,0"
           Width="16"/>
</ResourceDictionary>

Here the x:Share works fine.