0

我得到 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>

任何想法如何将多个 Pathgeometrys + RectangelGeometry 作为样式添加到我的按钮中?

4

1 回答 1

1

您可以使用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才能正确显示图标。)

于 2020-03-11T09:54:35.873 回答