Search code examples
wpfgeometry-path

How to group Paths in WPF?


I'm using paths to create a specific shape. What is the best way to group all the paths so I'll be able to use them later in another location without re-positioning the paths?

Any suggestions will be great, thank you!


Solution

  • If the question is about a complex shape with just one fill and stroke, an option would be to use just one Path instance with a PathGeometry with multiple Figures. See the following example from MSDN:

    <Path Stroke="Black" StrokeThickness="1">
        <Path.Data>
            <PathGeometry>
                <PathGeometry.Figures>
                    <PathFigureCollection>
                        <PathFigure IsClosed="True" StartPoint="10,100">
                            <PathFigure.Segments>
                                <PathSegmentCollection>
                                    <LineSegment Point="100,100" />
                                    <LineSegment Point="100,50" />
                                </PathSegmentCollection>
                            </PathFigure.Segments>
                        </PathFigure>
                        <PathFigure IsClosed="True" StartPoint="10,10">
                            <PathFigure.Segments>
                                <PathSegmentCollection>
                                    <LineSegment Point="100,10" />
                                    <LineSegment Point="100,40" />
                                </PathSegmentCollection>
                            </PathFigure.Segments>
                        </PathFigure>
                    </PathFigureCollection>
                </PathGeometry.Figures>
            </PathGeometry>
        </Path.Data>
    </Path>