Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why can't I use the same Icon for more than 1 item in Menu?

Tags:

wpf

menu

I have a MenuItem like below

<MenuItem Header="Edit">
    <MenuItem Header="Copy Direct Link" Icon="{StaticResource CopyIcon}" Command="{Binding CopyImageCommand}" />
    <MenuItem Header="Copy Image Data" Icon="{StaticResource CopyIcon}" Command="{Binding CopyImageDataCommand}" />
    <MenuItem Header="Paste" Icon="{StaticResource PasteIcon}" Command="{Binding PasteImageCommand}" />
</MenuItem>

Notice the 1st 2 items use the same icon, I get something like below

I tried removing the 2nd item,

<MenuItem Header="Edit">
    <MenuItem Header="Copy Direct Link" InputGestureText="Ctrl+C" Icon="{StaticResource CopyIcon}" Command="{Binding CopyImageCommand}" />
    <!--<MenuItem Header="Copy Image Data" InputGestureText="Ctrl+Alt+C" Icon="{StaticResource CopyIcon}" Command="{Binding CopyImageDataCommand}" />-->
    <MenuItem Header="Paste" InputGestureText="Ctrl+P" Icon="{StaticResource PasteIcon}" Command="{Binding PasteImageCommand}" />
</MenuItem>

then I got something like

How can I reuse Icons?

like image 502
Jiew Meng Avatar asked Nov 29 '10 11:11

Jiew Meng


1 Answers

See this question

An Image can only have one parent so it will be moved from the first MenuItem to the second. You can add the x:Shared attribute like this

<Window.Resources>
    <Image x:Key="CopyIcon" x:Shared="False" Source="..." />
</Window.Resources>

From msdn

x:Shared Attribute
When set to false, modifies WPF resource-retrieval behavior so that requests for the attributed resource create a new instance for each request instead of sharing the same instance for all requests.

like image 124
Fredrik Hedblad Avatar answered Oct 18 '22 20:10

Fredrik Hedblad