为什么我不能在菜单中为超过 1 个项目使用相同的图标? [英] Why can't I use the same Icon for more than 1 item in Menu?

查看:10
本文介绍了为什么我不能在菜单中为超过 1 个项目使用相同的图标?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个像下面这样的 MenuItem

<MenuItem Header="复制直接链接" Icon="{StaticResource CopyIcon}" Command="{Binding CopyImageCommand}"/><MenuItem Header="复制图像数据" Icon="{StaticResource CopyIcon}" Command="{Binding CopyImageDataCommand}"/><MenuItem Header="Paste" Icon="{StaticResource PasteIcon}" Command="{Binding PasteImageCommand}"/></菜单项>

注意第一个 2 个项目使用相同的图标,我得到如下所示的内容

我尝试删除第二项,

<MenuItem Header="复制直接链接" InputGestureText="Ctrl+C" Icon="{StaticResource CopyIcon}" Command="{Binding CopyImageCommand}"/><!--<MenuItem Header="复制图像数据" InputGestureText="Ctrl+Alt+C" Icon="{StaticResource CopyIcon}" Command="{Binding CopyImageDataCommand}"/>--><MenuItem Header="Paste" InputGestureText="Ctrl+P" Icon="{StaticResource PasteIcon}" Command="{Binding PasteImageCommand}"/></菜单项>

然后我得到了类似的东西

如何重复使用图标?

解决方案

这个问题>

一个图像只能有一个父项,因此它将从第一个 MenuItem 移动到第二个.您可以像这样添加 x:Shared 属性

<Image x:Key="CopyIcon" x:Shared="False" Source=".​​.."/></Window.Resources>

来自 msdn

<块引用>

x:共享属性
设置为 false 时,修改 WPF资源检索行为,以便对属性资源的请求为每个请求创建一个新实例而不是共享同一个实例对于所有请求.

I have a MenuItem like below

<MenuItem Header="Edit">
    <MenuItem Header="Copy Direct Link" Icon="{StaticResource CopyIcon}" Command="{Binding CopyImageCommand}" />
    <MenuItem Header="Copy Image Data" Icon="{StaticResource CopyIcon}" Command="{Binding CopyImageDataCommand}" />
    <MenuItem Header="Paste" Icon="{StaticResource PasteIcon}" Command="{Binding PasteImageCommand}" />
</MenuItem>

Notice the 1st 2 items use the same icon, I get something like below

I tried removing the 2nd item,

<MenuItem Header="Edit">
    <MenuItem Header="Copy Direct Link" InputGestureText="Ctrl+C" Icon="{StaticResource CopyIcon}" Command="{Binding CopyImageCommand}" />
    <!--<MenuItem Header="Copy Image Data" InputGestureText="Ctrl+Alt+C" Icon="{StaticResource CopyIcon}" Command="{Binding CopyImageDataCommand}" />-->
    <MenuItem Header="Paste" InputGestureText="Ctrl+P" Icon="{StaticResource PasteIcon}" Command="{Binding PasteImageCommand}" />
</MenuItem>

then I got something like

How can I reuse Icons?

解决方案

See this question

An Image can only have one parent so it will be moved from the first MenuItem to the second. You can add the x:Shared attribute like this

<Window.Resources>
    <Image x:Key="CopyIcon" x:Shared="False" Source="..." />
</Window.Resources>

From msdn

x:Shared Attribute
When set to false, modifies WPF resource-retrieval behavior so that requests for the attributed resource create a new instance for each request instead of sharing the same instance for all requests.

这篇关于为什么我不能在菜单中为超过 1 个项目使用相同的图标?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆