I have some images that I use as icons for ContextMenu Items:
<UserControl.Resources>
<Image x:Key="DeleteIco" Source="pack://application:,,,/MyProject.myControl;component/Resources/Delete.ico" Width="16" Height="16"/>
...
<ContextMenu x:Key="MyMenu1">
<MenuItem Header="Delete" Icon="{StaticResource DeleteIco}"/>
</ContextMenu>
<ContextMenu x:Key="MyMenu2">
<MenuItem Header="Delete me" Icon="{StaticResource DeleteIco}"/>
</ContextMenu>
<UserControl.Resources>
Sometimes the first menu miss the icon, sometimes the second… why? I don’t do anything on the icons in the code.
You have created an
Imagecontrol, and tried to put it twice in the visual tree. Since all controls can only have one parent yourImagegot disconnected. Thus using it a second time, the first will be disconnected, resulting in your missing icon.You can resolve this, by not adding the
Image, but theImageSourceinstead to your resources:Your menu has to change a bit for it to work though:
Update:
You can also use styles to set some basic properties of the
Imagefor youOr use a Style for
MenuItemto set theIconeach time.And the MenuItem: