Reusable polygon

I want to have canvas in xaml where I place some icons. These icons are polygons like this:

<Polygon Points="0,0 20,50, 0,50 20,0" Fill="Red" Stretch="Uniform"/>

But I want to use the icon several times, so I want to define it in the resources and include it by reference in the canvas at a certain position, one way or another:

<Page.Resources>
   <Polygon Key="icon1" Points="0,0 20,50, 0,50 20,0" Fill="Red" Stretch="Uniform"/>
   <Polygon Key="icon2" Points="0,0 10,30, 10,60 20,0" Fill="Blue" Stretch="Uniform"/>
   ...
</Page.Resources>    
<Canvas>
   <Polygon Reference="icon1" X="0" Y="0"/>
   <Polygon Reference="icon2" X="10" Y="10"/>   
   <Polygon Reference="icon1" X="20" Y="20"/>   
   ...          
</Canvas>

I found a possible solution at http://www.codeproject.com/KB/WPF/GraphicInXAMLAndWPF.aspx , where the polygons are stored in a graphic, but there seems to be a lot of overhead.

Does anyone have a better idea how to solve this?

+5
source share
1 answer

, UserControl. UserControl , LayoutRoot, Visual Studio. , !

, SO, , : x: Shared = "false", , .

<Page.Resources>
   <Polygon x:Key="icon1"  x:Shared="False"
            Points="0,0 20,50, 0,50 20,0" Fill="Red" Stretch="Uniform"/>
   <Polygon x:Key="icon2"  x:Shared="False"
            Points="0,0 10,30, 10,60 20,0" Fill="Blue" Stretch="Uniform"/>
   ...
</Page.Resources>    
<Canvas>
    <ContentControl Content="{StaticResource icon1}" Canvas.Top="0" Canvas.Left="0"/>
    <ContentControl Content="{StaticResource icon2}" Canvas.Top="0" Canvas.Left="10"/>
    <ContentControl Content="{StaticResource icon1}" Canvas.Top="0" Canvas.Left="20"/>
   ...          
</Canvas>

. :

XAML

+7

All Articles