Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Reuse path object in XAML

Tags:

c#

.net

wpf

xaml

I have a Path (a star figure):

<Path x:Name="NiceStar" StrokeThickness="10" Stroke="#ff000000" StrokeMiterLimit="1" Data="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">
    <Path.Fill>
        <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>
    </Path.Fill>
</Path>

Now I want to duplicate this Path several times (just refering to "NiceStar"). Can I do this in pure XAML?

I can use it once, by doing this:

<Decorator Child="{StaticResource star}" />

However, I cannot duplicate this line. My compiler says:

Specified element is already the logical child of another element. Disconnect it first.

like image 320
Robbert Dam Avatar asked May 08 '09 13:05

Robbert Dam


1 Answers

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}"/>
like image 150
Josh G Avatar answered Sep 18 '22 06:09

Josh G