WPF 右键菜单 MenuItem 绑定图片时只显示最后一个 Icon

在做一个项目时需要给右键菜单动态绑定项时,其它内容都是正常的,绑定图片就有最后一个图标有图片。

XML 复制代码
        <MenuItem.ItemContainerStyle>
         <Style TargetType="MenuItem">
          <Setter Property="Icon">
           <Setter.Value>
            <Image Height="16" Width="16" Stretch="Uniform" 
                   Source="{Binding ImageName,Converter={StaticResource StringToResImageSource}}"/>
           </Setter.Value>
          </Setter>
          <Setter Property="Header" Value="{Binding Name}"/>
          <Setter Property="Command" Value="{Binding ReplaceCommand}" />
          <Setter Property="CommandParameter" Value="{Binding RelativeSource={RelativeSource Mode=FindAncestor, AncestorType=ContextMenu}, Path=PlacementTarget.SelectedItems}" />
         </Style>
        </MenuItem.ItemContainerStyle>

这时想着用改成使用资源来显示,发现也是只显示最后一张图片。后来在网上看到说把图像改成资源然后把 x:Shared="false" 设置成假就能成功。经过修改终于能显示出来了。

XML 复制代码
 <Image x:Key="img_mitem" x:Shared="false"
        Width="16px" Height="16px"
        Stretch="Uniform" Source="{Binding ImageName,Converter={StaticResource StringToResImageSource}}" />
XML 复制代码
 <Setter Property="Icon" Value="{StaticResource img_mitem}"/>

修改后的效果:

相关推荐
没有bug.的程序员9 小时前
服务治理与 API 网关:微服务流量管理的艺术
java·分布式·微服务·架构·wpf
Brianna Home14 小时前
【案例实战】鸿蒙分布式调度:跨设备协同实战
华为·wpf·harmonyos
c#上位机2 天前
wpf中Grid的MouseDown 事件无法触发的原因
c#·wpf
△曉風殘月〆2 天前
如何在WPF中实现ComboBox多选
wpf
csdn_aspnet2 天前
如何使用现有工具进行 .NET 8 迁移 Wpf
wpf·.net 8
123梦野2 天前
WPF——动画
wpf
攻城狮CSU3 天前
WPF 疑点汇总2.HorizontalAlignment和 HorizontalContentAlignment
wpf
c#上位机3 天前
wpf之命令
c#·wpf
没有bug.的程序员3 天前
分布式链路追踪:微服务可观测性的核心支柱
java·分布式·微服务·架构·wpf
Aevget3 天前
DevExpress WPF中文教程:Data Grid - 如何使用虚拟源?(一)
c#·wpf·界面控件·devexpress·ui开发