在XAML中重用路径对象

Rob*_*Dam 14 .net c# wpf xaml

我有一个Path(一个明星人物):

<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>
Run Code Online (Sandbox Code Playgroud)

现在我想多次复制这个Path(只是引用"NiceStar").我可以在纯XAML中执行此操作吗?

通过这样做,我可以使用它一次:

<Decorator Child="{StaticResource star}" />
Run Code Online (Sandbox Code Playgroud)

但是,我不能复制这一行.我的编译器说:

指定的元素已经是另一个元素的逻辑子元素.首先断开它.

Jos*_*h G 23

创造一种风格.

<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>
Run Code Online (Sandbox Code Playgroud)

...

<Path Style="{StaticResource NiceStarPath}"/>
Run Code Online (Sandbox Code Playgroud)


Mar*_*ath 7

当然,只需为路径定义样式,然后您可以将其重用为静态资源:

<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>
Run Code Online (Sandbox Code Playgroud)


Dre*_*hie 6

在相关说明中,(尽管可能没有直接回答您的问题),您也可以将FrameworkElement声明为资源,为其指定密钥,只要添加,x:Shared="False"您就可以在代码中反复访问资源.

这是一个伪编码的例子:

<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>
Run Code Online (Sandbox Code Playgroud)

然后,在代码中,您可以访问资源化的形状并根据需要重复使用它.

Ellipse tempRect = (Ellipse)FindResouce("ReusableEllipse");
Run Code Online (Sandbox Code Playgroud)