Search code examples
c#wpfxamlpathpathgeometry

WPF: XAML DrawingImage to Path.Style


I get my Icons in XAML-format, like this (path shortened for better readability):

<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>

Then I extract the Pathgeometry and put it into a path-style:

<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>

Then I can Inject any of my Icons into a Button-content (and also its color), like this:

 <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>

That works really well, even though I lose the RectangleGeometry. My problem is that this only works with Icons that have only 1 PathGeometry included. I don't know how to convert it if I have a XAML Icon like this with multiple paths that also depend on the rectanglegeometry:

<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>

any Ideas how I can get multiple Pathgeometrys + the RectangelGeometry as a Style into my button?


Solution

  • You can use a 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>
    

    (I had to use Stroke instead of Fill though for the icon to be displayed correctly.)