只有最后一个MenuItem才能获得Icon

时间:2013-10-07 21:42:41

标签: wpf styles .net-4.5 multitrigger

我知道还有其他线索,但就我而言,它有点不同。

我喜欢使用来自单独资源程序集的图标

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

这在

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

我尝试了x:分享,但这在ResourceDictionary中没有使用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>

有人有想法解决这个问题。将图标分别添加到任何条目并没有为我解决问题,因为在我的应用程序中大约有200个项目。

最好的问候

1 个答案:

答案 0 :(得分:2)

正确的资源解决了这个问题。

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

这里x:Share工作正常。