C# 在 XAML 中重用路径对象
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/839843/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Reuse path object in XAML
提问by Robbert Dam
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?
现在我想多次复制此路径(仅指“NiceStar”)。我可以在纯 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.
指定的元素已经是另一个元素的逻辑子元素。先断开它。
采纳答案by Josh G
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}"/>
回答by Mark Heath
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>
回答by Drew McGhie
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.
在相关说明中,(虽然可能不会直接回答您的问题),您还可以将 FrameworkElement 声明为 Resource,给它一个键,只要添加x:Shared="False"
您就可以在代码中一次又一次地访问该资源。
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");
回答by Rob Fonseca-Ensor
I would turn the path into a DrawingBrush. This is really easy to do in blend, just select the path, Tools > Make Brush Resource > Make DrawingBrush Resource. Then you'll have the brush in your resources, ready to reuse. I expect the performance of this will be pretty good, since the brush is non-interactive and reusable.
我会将路径变成DrawingBrush。这在混合中很容易做到,只需选择路径,工具>制作画笔资源>制作画笔资源。然后,您将拥有资源中的画笔,可以重复使用。我希望它的性能会非常好,因为画笔是非交互式且可重复使用的。
Here is the XAML:
这是 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>
Another option is to turn wrap the path into an imagesource using DrawingImage
另一种选择是使用DrawingImage将路径包装成图像源
回答by ezolotko
You can use style with a control template for this
您可以为此使用带有控件模板的样式
<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>
And then use it:
然后使用它:
<Button>
<Control Style="{StaticResource FolderIcon}"/>
</Button>
回答by Martin
I defined a path in separate XAML document and then reused it as content of a ContentControl.
我在单独的 XAML 文档中定义了一个路径,然后将其作为 ContentControl 的内容重用。
In the resource file or resource dictionary:
在资源文件或资源字典中:
<Path x:Key="SearchIcon"
Width="30" Height="30" Margin="3" Fill="Blue"
Data="Path data here"
Stretch="Fill" />
In the WPF control:
在 WPF 控件中:
<ContentControl Content="{StaticResource SearchIcon}"></ContentControl>