【问题标题】:WPF: XAML DrawingImage to Path.StyleWPF:XAML DrawingImage 到 Path.Style
【发布时间】:2020-06-23 03:07:36
【问题描述】:

我得到 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 作为样式添加到我的按钮中?

【问题讨论】:

    标签: c# wpf xaml path pathgeometry


    【解决方案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 才能正确显示图标。)

    【讨论】:

    • 您知道是否可以让 GeometryGroup 中的每个 PathGeometry 具有不同的 StrokeColor / StrokeThickness?
    • 不,我认为这不可能……您必须为 like in this answer 使用多个路径。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-05-21
    • 2010-12-25
    • 1970-01-01
    相关资源
    最近更新 更多