我得到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>然后提取路径几何并将其放入路径样式:
<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,但这确实很好。我的问题是,这只适用于只包含一个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>有什么想法吗?我怎样才能让多个路径几何+ RectangelGeometry作为样式进入我的按钮?
发布于 2020-03-11 09:54:35
您可以使用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。)
https://stackoverflow.com/questions/60630833
复制相似问题