Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ContentControls referencing the same StaticResource only render the resource once

Tags:

wpf

I have a resource in a ResourceDictionary:

<Viewbox x:Key="Icons.Profile">
    <Canvas Width="76" Height="76" Clip="F1 M 0,0L 76,0L 76,76L 0,76L 0,0">
        <Path Width="40.7907" Height="33.55" Canvas.Left="16.2093" Canvas.Top="22.2" Stretch="Fill" Fill="#FFFFFFFF" Data="F1 M ... "/>
    </Canvas>
</Viewbox>

If I add two Buttons/Content Controls that reference the StaticResource only the one rendered last seems to render the "Icon"

<Button Content="{StaticResource Icons.Profile}" />

Does anyone know why and how I can get around this? In Visual Studio and Blend the Content is being rendered for all controls.

like image 745
Allan Smith Avatar asked Aug 13 '14 00:08

Allan Smith


1 Answers

This is because a element can only have 1 logical parent, however you can set your resource to x:Shared="false" and this will create/render a new element for each use.

Example:

<Viewbox x:Key="Icons.Profile" x:Shared="false">
    <Canvas Width="76" Height="76" Clip="F1 M 0,0L 76,0L 76,76L 0,76L 0,0">
        <Path Width="40.7907" Height="33.55" Canvas.Left="16.2093" Canvas.Top="22.2" Stretch="Fill" Fill="#FFFFFFFF" Data="F1 M ... "/>
    </Canvas> 
</Viewbox>
like image 185
sa_ddam213 Avatar answered Oct 23 '22 18:10

sa_ddam213