Why can't I use the same icon for more than 1 menu item?

I have MenuItemas 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>

Please note that the first 2 elements use the same icon, I get something like below

I tried to remove the second element,

<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?

+3
source share
3 answers

See this question

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

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

From msdn

x:
false, WPF , , .

+8

, CopyIcon , :

<Window.Resources>
  <Image x:Key="CopyIcon" Source="yourcopyicon.ico"/>
</Window.Resources>

, : Image - , FrameworkElement ( ), . MenuItem , MenuItem reset CopyIcon, CopyIcon.

, . Meleak .: -)

+4

Try the following:

<MenuItem Header="Paste" >
<MenuItem.Icon><Image Height="16" Width="16" Source="paste.jpg" /></MenuItem.Icon>
</MenuItem>
+1
source

Source: https://habr.com/ru/post/1777442/


All Articles