我有一个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>
现在我想多次复制这个Path(只是引用“NiceStar”)。我可以用纯XAML吗?
通过这样做,我可以使用它一次:
<Decorator Child="{StaticResource star}" />
但是,我不能复制这一行。我的编译器说:
指定的元素已经是另一个元素的逻辑子元素。首先断开它。
答案 0 :(得分: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>
...
<Path Style="{StaticResource NiceStarPath}"/>
答案 1 :(得分: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>
答案 2 :(得分: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>
然后,在代码中,您可以访问资源化形状并根据需要多次重复使用。
Ellipse tempRect = (Ellipse)FindResouce("ReusableEllipse");
答案 3 :(得分:3)
我会把路径变成DrawingBrush。这在混合中非常容易,只需选择路径,工具&gt;制作画笔资源&gt;制作DrawingBrush资源。然后,您将拥有资源中的画笔,随时可以重复使用。我希望这个性能非常好,因为画笔是非交互式的并且可以重复使用。
这是XAML:
<Window
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Width="640" Height="480">
<Window.Resources>
<DrawingBrush x:Key="NiceStarBrush" Viewbox="0,0,250,240" ViewboxUnits="Absolute">
<DrawingBrush.Drawing>
<GeometryDrawing Geometry="F1M126.578613,11.297852L162.373535,83.825684 242.412598,95.456055 184.495605,151.911133 198.16748,231.626953 126.578613,193.990234 54.98877,231.626953 68.661621,151.911133 10.744629,95.456055 90.783691,83.825684 126.578613,11.297852z">
<GeometryDrawing.Brush>
<RadialGradientBrush MappingMode="Absolute" Center="390.395508,448.130371" GradientOrigin="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 Color="Lime" Offset="0"/>
<GradientStop Color="#FF006736" Offset="1"/>
</RadialGradientBrush>
</GeometryDrawing.Brush>
<GeometryDrawing.Pen>
<Pen Brush="Black" DashCap="Flat" EndLineCap="Flat" LineJoin="Miter" MiterLimit="1" StartLineCap="Flat" Thickness="10">
<Pen.DashStyle>
<DashStyle/>
</Pen.DashStyle>
</Pen>
</GeometryDrawing.Pen>
</GeometryDrawing>
</DrawingBrush.Drawing>
</DrawingBrush>
</Window.Resources>
<Grid x:Name="LayoutRoot">
<Rectangle Margin="181,115,0,0" Fill="{DynamicResource NiceStarBrush}" HorizontalAlignment="Left" VerticalAlignment="Top" Width="50" Height="46" />
</Grid>
</Window>
另一种选择是使用DrawingImage
将路径包装成图像源答案 4 :(得分:2)
您可以将样式与控件模板一起用于此
<Style TargetType="Control" x:Key="FolderIcon">
<Setter Property="IsTabStop" Value="False"/>
<Setter Property="Template">
<Setter.Value>
<ControlTemplate>
<Path Data="M -1,0 h 5 M 0,3 h 10 v 5 h -10 Z" StrokeThickness="2" Stroke="White" Fill="White" />
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
然后使用它:
<Button>
<Control Style="{StaticResource FolderIcon}"/>
</Button>
答案 5 :(得分:0)
我在单独的XAML文档中定义了一个路径,然后将其重新用作ContentControl的内容。
在资源文件或资源字典中:
<Path x:Key="SearchIcon"
Width="30" Height="30" Margin="3" Fill="Blue"
Data="Path data here"
Stretch="Fill" />
在WPF控件中:
<ContentControl Content="{StaticResource SearchIcon}"></ContentControl>
答案 6 :(得分:0)
如果你想多次重用路径数据,也许在多个样式之间,你可以定义一个 Geometry 资源:
<ResourceDictionary>
<Geometry x:Key="StarPathData">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</Geometry>
</ResourceDictionary>
然后直接使用:
<Path Data="{StaticResource StarPathData}">
或风格:
<Style x:Key="NiceStarPath" TargetType="{x:Type Path}">
<Setter Property="Data" Value="{StaticResource StarPathData}"/>
...
</Style>