WPF: Disappearing Icons

I have several icons that are declared in Window.Resources. They are displayed for the first time when they need to appear (for example: clicking on the Menu, MenuItem icon), but after displaying another menu (for example: context menu), the initial icon disappears and does not return. As if the last item that used the icon for the first time gets 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 tried to save it as 24-bit PNG, interlaced 24-bit PNG and 8-bit PNG, but the same thing happens. This is not only one, each icon that is used in several places behaves this way.

+7
wpf icons menuitem
source share
1 answer

This is because your Image resource, which is Control . Control can have only one parent, so it is effectively updated in every MenuItem on the fly.

Your options:

  • Do not use Image and instead use an ImageSource or even a string containing the URI of the image.
  • Define a resource that does not share the x:Shared XAML attribute. If necessary, several Image controls will be created.
+9
source share

All Articles