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}"/>

修改后的效果:

相关推荐
samble2 天前
从零搭建半导体设备上位机(十二):参数设置页,表单校验、范围校验与配方联动
wpf·数据校验·validationrule
samble11 天前
从零搭建半导体设备上位机(九):拖拽与交互,Adorner、DragDrop 与晶圆拖放信息
wpf·拖拽·adorner
郝学胜-神的一滴12 天前
AI 编程智能体 27:吃透MCP通信机制
人工智能·python·langchain·wpf·numpy·scikit-learn
波力海苔夹心脆67513 天前
C# WPF 资源字典详解:ResourceDictionary、MergedDictionaries 与自定义控件案例
开发语言·c#·wpf·入门教程·resource·资源字典
陈仓主教13 天前
# WPF CheckBox 控件详解与使用—WPF 中的控件(6)--复选框控件(CheckBox)
wpf
光泽雨15 天前
WPF 音视频四大核心方案对比
wpf·音视频
浪扼飞舟16 天前
WPF(数据绑定)
wpf
samble17 天前
从零搭建半导体设备上位机(四):单实例、启动流程与配置文件,App 里那 526 行到底在干什么
wpf·配置文件·单实例
浪扼飞舟17 天前
WPF(控件的继承树、内容控件、布局控件)
wpf
avi911120 天前
电影开源Jevdo - 源码介绍(vue版)没有android 和 没有wpf
android·wpf·框架·数据爬虫·meego·电影管理