Reuse path object in XAML

Create a style.

<Style x:Key="NiceStarPath" TargetType="{x:Type Path}">
    <Setter Property="StrokeThickness" Value="10"/>
    <Setter Property="Stroke" Value="#FF000000"/>
    <Setter Property="StrokeMiterLimit" Value="1"/>
    <Setter Property="Data" Value="F1 M 126.578613,11.297852 L 162.373535,83.825684 L 242.412598,95.456055 L 184.495605,151.911133 L 198.167480,231.626953 L 126.578613,193.990234 L 54.988770,231.626953 L 68.661621,151.911133 L 10.744629,95.456055 L 90.783691,83.825684 L 126.578613,11.297852 Z"/>
    <Setter Property="Fill">
        <Setter.Value>
            <RadialGradientBrush MappingMode="Absolute" GradientOrigin="390.395508,448.130371" Center="390.395508,448.130371" RadiusX="113.034821" RadiusY="113.034821">
                <RadialGradientBrush.Transform>
                    <MatrixTransform Matrix="1,0,-0,-1,-263.816895,569.592773" />
                </RadialGradientBrush.Transform>
                <GradientStop Offset="0" Color="#ff00ff00"/>
                <GradientStop Offset="1" Color="#ff006736"/>
            </RadialGradientBrush>
        </Setter.Value>
    </Setter>
</Style>

...

<Path Style="{StaticResource NiceStarPath}"/>

Sure, just define a Style for the path and then you can reuse it as a static resource:

<Page
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
<Page.Resources>
    <Style x:Key="StarStyle" TargetType="Path">
    <Setter>
        <Setter.Property>Fill</Setter.Property>
        <Setter.Value>                
             <RadialGradientBrush MappingMode="Absolute"
              GradientOrigin="390.395508,448.130371" Center="390.395508,448.130371"
              RadiusX="113.034821" RadiusY="113.034821">
             <RadialGradientBrush.Transform>
                 <MatrixTransform Matrix="1,0,-0,-1,-263.816895,569.592773" />
             </RadialGradientBrush.Transform>
             <GradientStop Offset="0" Color="#ff00ff00"/>
             <GradientStop Offset="1" Color="#ff006736"/>
             </RadialGradientBrush>
         </Setter.Value> 
    </Setter>
    <Setter Property="StrokeThickness" Value="10" />
    <Setter Property="Stroke" Value="#ff000000" />
    <Setter Property="StrokeMiterLimit" Value="1" />
    <Setter Property="Data" Value="F1 M 126.578613,11.297852 L 162.373535,83.825684 L 242.412598,95.456055 L 184.495605,151.911133 L 198.167480,231.626953 L 126.578613,193.990234 L 54.988770,231.626953 L 68.661621,151.911133 L 10.744629,95.456055 L 90.783691,83.825684 L 126.578613,11.297852 Z"/>
    </Style>
</Page.Resources>
<StackPanel>  
    <Path Style="{StaticResource StarStyle}" />
    <Path Style="{StaticResource StarStyle}" />
</StackPanel>
</Page>

In a related note, (although probably not directly answering your question), you can also declare a FrameworkElement as a Resource, give it a key, and as long as you add x:Shared="False" you can access the resource again and again in code.

Here's a pseudocoded example:

<Window ....>
   <Window.Resources>
      <Ellipse x:Key="ReusableEllipse" x:Shared="False" ...>
         <Ellipse.Fill>
            <!--STUFF-->
         </Ellipse.Fill>
      </Ellipse>
   </Window.Resources>
   <Canvas x:Name="drawCanvas" Background="White"/>
</Window>

Then, in code, you can access the resourced shape and reuse it as many times as needed.

Ellipse tempRect = (Ellipse)FindResouce("ReusableEllipse");

Tags:

C#

.Net

Wpf

Xaml