WPF:XAML DrawingImage 到 Path.Style
WPF: XAML DrawingImage to Path.Style
我得到了 XAML 格式的图标,像这样(为了更好的可读性缩短了路径):
<DrawingImage x:Key="IcoNew">
<DrawingImage.Drawing>
<DrawingGroup>
<DrawingGroup.Children>
<DrawingGroup>
<DrawingGroup.Children>
<GeometryDrawing Brush="{StaticResource WarningForeground}" Pen="{x:Null}">
<GeometryDrawing.Geometry>
<PathGeometry FillRule="Nonzero" Figures="M20,4L4,4C2.895,4,2,4.895,........" />
</GeometryDrawing.Geometry>
</GeometryDrawing>
</DrawingGroup.Children>
<DrawingGroup.ClipGeometry>
<RectangleGeometry Rect="0,0,24,24" />
</DrawingGroup.ClipGeometry>
</DrawingGroup>
</DrawingGroup.Children>
</DrawingGroup>
</DrawingImage.Drawing>
</DrawingImage>
然后我提取 Pathgeometry 并将其放入路径样式中:
<Style x:Key="IcoNew" TargetType="{x:Type Path}">
<Setter Property="Stretch" Value="Uniform"/>
<Setter Property="Fill" Value="{StaticResource MainAccentColor}"/>
<Setter Property="Data">
<Setter.Value>
<PathGeometry FillRule="Nonzero" Figures="M20,4L4,4C2.895,4,2,4.895,........" />
</Setter.Value>
</Setter>
</Style>
然后我可以将我的任何图标注入按钮内容(及其颜色),如下所示:
<Button.Template>
<ControlTemplate>
<Grid Height="{Binding Path=MaxWidth, RelativeSource={RelativeSource FindAncestor,AncestorType={x:Type Button}}}" Width="{Binding Path=MaxWidth, RelativeSource={RelativeSource FindAncestor,AncestorType={x:Type Button}}}">
<Path Name="vbx" Fill="{Binding Path=Foreground, TargetNullValue={StaticResource SidebarIconColor}, RelativeSource={RelativeSource FindAncestor,AncestorType={x:Type Button}}}"
Height="{Binding Path=MinWidth, RelativeSource={RelativeSource FindAncestor,AncestorType={x:Type Button}}}"
Width="{Binding Path=MinWidth, RelativeSource={RelativeSource FindAncestor,AncestorType={x:Type Button}}}"
Style="{Binding Path=Content, RelativeSource={RelativeSource FindAncestor,AncestorType={x:Type Button}}}"/>
</Grid>
</ControlTemplate>
</Button.Template>
虽然我丢失了 RectangleGeometry,但效果非常好。
我的问题是这仅适用于仅包含 1 个 PathGeometry 的图标。如果我有一个像这样的 XAML 图标有多个路径也取决于矩形几何,我不知道如何转换它:
<DrawingImage x:Key="Icons8_Attach_11321">
<DrawingImage.Drawing>
<DrawingGroup>
<DrawingGroup.Children>
<DrawingGroup>
<DrawingGroup.Children>
<GeometryDrawing Brush="{x:Null}">
<GeometryDrawing.Pen>
<Pen Brush="#FFFFFFFF" Thickness="2" />
</GeometryDrawing.Pen>
<GeometryDrawing.Geometry>
<PathGeometry FillRule="Nonzero" Figures="M14.742,28.793C13.184,30.352 13.184,32.878 14.742,34.436 16.3,35.995 18.826,35.995 20.384,34.436L35.488,19.332" />
</GeometryDrawing.Geometry>
</GeometryDrawing>
<GeometryDrawing Brush="{x:Null}">
<GeometryDrawing.Pen>
<Pen Brush="#FFFFFFFF" Thickness="2" />
</GeometryDrawing.Pen>
<GeometryDrawing.Geometry>
<PathGeometry FillRule="Nonzero" Figures="M27.025,10.869L7.336,30.557C4.221,33.672 4.221,38.727 7.337,41.842 10.453,44.957 15.506,44.957 18.621,41.842L43.248,17.216C45.584,14.88 45.584,11.09 43.248,8.753 40.91,6.415 37.119,6.416 34.783,8.753L14.742,28.793" />
</GeometryDrawing.Geometry>
</GeometryDrawing>
</DrawingGroup.Children>
<DrawingGroup.ClipGeometry>
<RectangleGeometry Rect="0,0,50,50" />
</DrawingGroup.ClipGeometry>
</DrawingGroup>
</DrawingGroup.Children>
</DrawingGroup>
</DrawingImage.Drawing>
</DrawingImage>
有什么想法可以让多个 Pathgeometry + RectangelGeometry 作为样式进入我的按钮吗?
您可以使用 GeometryGroup
:
<Setter Property="Data">
<Setter.Value>
<GeometryGroup>
<PathGeometry FillRule="Nonzero" Figures="M14.742,28.793C13.184,30.352 13.184,32.878 14.742,34.436 16.3,35.995 18.826,35.995 20.384,34.436L35.488,19.332" />
<PathGeometry FillRule="Nonzero" Figures="M27.025,10.869L7.336,30.557C4.221,33.672 4.221,38.727 7.337,41.842 10.453,44.957 15.506,44.957 18.621,41.842L43.248,17.216C45.584,14.88 45.584,11.09 43.248,8.753 40.91,6.415 37.119,6.416 34.783,8.753L14.742,28.793" />
</GeometryGroup>
</Setter.Value>
</Setter>
(我必须使用 Stroke
而不是 Fill
才能正确显示图标。)
我得到了 XAML 格式的图标,像这样(为了更好的可读性缩短了路径):
<DrawingImage x:Key="IcoNew">
<DrawingImage.Drawing>
<DrawingGroup>
<DrawingGroup.Children>
<DrawingGroup>
<DrawingGroup.Children>
<GeometryDrawing Brush="{StaticResource WarningForeground}" Pen="{x:Null}">
<GeometryDrawing.Geometry>
<PathGeometry FillRule="Nonzero" Figures="M20,4L4,4C2.895,4,2,4.895,........" />
</GeometryDrawing.Geometry>
</GeometryDrawing>
</DrawingGroup.Children>
<DrawingGroup.ClipGeometry>
<RectangleGeometry Rect="0,0,24,24" />
</DrawingGroup.ClipGeometry>
</DrawingGroup>
</DrawingGroup.Children>
</DrawingGroup>
</DrawingImage.Drawing>
</DrawingImage>
然后我提取 Pathgeometry 并将其放入路径样式中:
<Style x:Key="IcoNew" TargetType="{x:Type Path}">
<Setter Property="Stretch" Value="Uniform"/>
<Setter Property="Fill" Value="{StaticResource MainAccentColor}"/>
<Setter Property="Data">
<Setter.Value>
<PathGeometry FillRule="Nonzero" Figures="M20,4L4,4C2.895,4,2,4.895,........" />
</Setter.Value>
</Setter>
</Style>
然后我可以将我的任何图标注入按钮内容(及其颜色),如下所示:
<Button.Template>
<ControlTemplate>
<Grid Height="{Binding Path=MaxWidth, RelativeSource={RelativeSource FindAncestor,AncestorType={x:Type Button}}}" Width="{Binding Path=MaxWidth, RelativeSource={RelativeSource FindAncestor,AncestorType={x:Type Button}}}">
<Path Name="vbx" Fill="{Binding Path=Foreground, TargetNullValue={StaticResource SidebarIconColor}, RelativeSource={RelativeSource FindAncestor,AncestorType={x:Type Button}}}"
Height="{Binding Path=MinWidth, RelativeSource={RelativeSource FindAncestor,AncestorType={x:Type Button}}}"
Width="{Binding Path=MinWidth, RelativeSource={RelativeSource FindAncestor,AncestorType={x:Type Button}}}"
Style="{Binding Path=Content, RelativeSource={RelativeSource FindAncestor,AncestorType={x:Type Button}}}"/>
</Grid>
</ControlTemplate>
</Button.Template>
虽然我丢失了 RectangleGeometry,但效果非常好。 我的问题是这仅适用于仅包含 1 个 PathGeometry 的图标。如果我有一个像这样的 XAML 图标有多个路径也取决于矩形几何,我不知道如何转换它:
<DrawingImage x:Key="Icons8_Attach_11321">
<DrawingImage.Drawing>
<DrawingGroup>
<DrawingGroup.Children>
<DrawingGroup>
<DrawingGroup.Children>
<GeometryDrawing Brush="{x:Null}">
<GeometryDrawing.Pen>
<Pen Brush="#FFFFFFFF" Thickness="2" />
</GeometryDrawing.Pen>
<GeometryDrawing.Geometry>
<PathGeometry FillRule="Nonzero" Figures="M14.742,28.793C13.184,30.352 13.184,32.878 14.742,34.436 16.3,35.995 18.826,35.995 20.384,34.436L35.488,19.332" />
</GeometryDrawing.Geometry>
</GeometryDrawing>
<GeometryDrawing Brush="{x:Null}">
<GeometryDrawing.Pen>
<Pen Brush="#FFFFFFFF" Thickness="2" />
</GeometryDrawing.Pen>
<GeometryDrawing.Geometry>
<PathGeometry FillRule="Nonzero" Figures="M27.025,10.869L7.336,30.557C4.221,33.672 4.221,38.727 7.337,41.842 10.453,44.957 15.506,44.957 18.621,41.842L43.248,17.216C45.584,14.88 45.584,11.09 43.248,8.753 40.91,6.415 37.119,6.416 34.783,8.753L14.742,28.793" />
</GeometryDrawing.Geometry>
</GeometryDrawing>
</DrawingGroup.Children>
<DrawingGroup.ClipGeometry>
<RectangleGeometry Rect="0,0,50,50" />
</DrawingGroup.ClipGeometry>
</DrawingGroup>
</DrawingGroup.Children>
</DrawingGroup>
</DrawingImage.Drawing>
</DrawingImage>
有什么想法可以让多个 Pathgeometry + RectangelGeometry 作为样式进入我的按钮吗?
您可以使用 GeometryGroup
:
<Setter Property="Data">
<Setter.Value>
<GeometryGroup>
<PathGeometry FillRule="Nonzero" Figures="M14.742,28.793C13.184,30.352 13.184,32.878 14.742,34.436 16.3,35.995 18.826,35.995 20.384,34.436L35.488,19.332" />
<PathGeometry FillRule="Nonzero" Figures="M27.025,10.869L7.336,30.557C4.221,33.672 4.221,38.727 7.337,41.842 10.453,44.957 15.506,44.957 18.621,41.842L43.248,17.216C45.584,14.88 45.584,11.09 43.248,8.753 40.91,6.415 37.119,6.416 34.783,8.753L14.742,28.793" />
</GeometryGroup>
</Setter.Value>
</Setter>
(我必须使用 Stroke
而不是 Fill
才能正确显示图标。)