WPF:消失的图标 [英] WPF: Disappearing icons
问题描述
我有几个在Window.Resources中声明的图标。它们在第一次出现时显示正常(例如:单击菜单,MenuItem图标有效),但在显示另一个菜单(例如:上下文菜单)后,原始图标消失且不返回。好像第一次使用该图标的最后一个元素就是保留它。
I have several icons which are declared in Window.Resources. They show up fine the first time they need to appear (eg: a Menu is clicked, the MenuItem icon works), but after another Menu (eg: a context menu) is shown, the original icon disappears and does not return. It's as though the last element which used the icon for the first time gets to keep it.
<Window.Resources>
<Image x:Key="Chart_16"
Source="pack://application:,,,/Resources/images/chart_16.png" />
...
<Window.Resources>
<MenuItem Header="Summary"
Command="loc:AppCommands.ShowSummary"
Icon="{StaticResource Chart_16}" />
我尝试将其保存为24位PNG,隔行扫描24位PNG和8位PNG但是同样的事情发生了这不仅仅是一个,在多个地方使用的每个图标都是这样的。
I've tried saving it as a 24bit PNG, an interlaced 24bit PNG and an 8bit PNG but the same thing happens. It's not just one, every icon which is used in more than one place behaves this way.
推荐答案
那是因为你的资源是一个 Image
,这是 Control
。 控制
s只能有一个父级,因此它可以动态地在每个 MenuItem
中重新设置父级。
That's because your resource is an Image
, which is a Control
. Control
s can only have one parent so it's effectively being re-parented in each MenuItem
on the fly.
您的选择是:
- 不要使用
Image
而是使用ImageSource
或甚至包含图像URI的字符串
。 - 使用
x:Shared
XAML属性将资源设置为非共享。这将根据需要创建多个图像
控件。
- Don't use
Image
and instead useImageSource
or even astring
containing the URI of the image. - Set the resource to non-shared with the
x:Shared
XAML attribute. This will create multipleImage
controls as needed.
这篇关于WPF:消失的图标的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!