views:

225

answers:

1

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.

+5  A: 

That's because your resource is an Image, which is a Control. Controls can only have one parent so it's effectively being re-parented in each MenuItem on the fly.

Your options are:

  1. Don't use Image and instead use ImageSource or even a string containing the URI of the image.
  2. Set the resource to non-shared with the x:Shared XAML attribute. This will create multiple Image controls as needed.

HTH, Kent

Kent Boogaart
Thanks, the x:Shared attribute did the trick.
Echilon