WPF:消失的图标
我有几个在 Window.Resources 中声明的图标。 它们在第一次需要出现时显示良好(例如:单击菜单,MenuItem 图标起作用),但在显示另一个菜单(例如:上下文菜单)后,原始图标消失并且不会返回。 就好像最后一个第一次使用该图标的元素保留了它。
<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 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}" />
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.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
这是因为您的资源是一个
Image
,它是一个Control
。Control
只能有一个父级,因此它可以在每个MenuItem
中有效地动态重新设置父级。您的选择是:
Image
,而是使用ImageSource
甚至包含图像 URI 的string
。x:Shared
XAML 属性将资源设置为非共享。 这将根据需要创建多个Image
控件。That's because your resource is an
Image
, which is aControl
.Control
s can only have one parent so it's effectively being re-parented in eachMenuItem
on the fly.Your options are:
Image
and instead useImageSource
or even astring
containing the URI of the image.x:Shared
XAML attribute. This will create multipleImage
controls as needed.