BlackWaspTM

This web site uses cookies. By using the site you accept the cookie policy.This message is for compliance with the UK ICO law.

Windows Presentation Foundation
.NET 4.0+

WPF Shapes - Path - Combined Geometry

The one hundred and twenty-sixth part of the Windows Presentation Foundation Fundamentals tutorial considers a second way to create complex geometries within a Path shape. This instalment looks at combined geometries, which merge two shapes into one, possibly changing the outline.

Combining Multiple Geometries

The CombinedGeometry class only has properties for two child shapes. However, as these can hold any type of geometry, you can overcome the limitation by nesting combined geometries.

The final example demonstrates this by combining three shapes. The inner geometry contains two ellipses, combined with a union. These are subtracted from a rectangle using the exclude option.

<Path Stroke="Black" StrokeThickness="5" Fill="HotPink">
    <Path.Data>
        <CombinedGeometry GeometryCombineMode="Exclude">
            <CombinedGeometry.Geometry1>
                <RectangleGeometry Rect="10,40 210,80" />
            </CombinedGeometry.Geometry1>
            <CombinedGeometry.Geometry2>
                <CombinedGeometry GeometryCombineMode="Union">
                    <CombinedGeometry.Geometry1>
                        <EllipseGeometry Center="0,40" RadiusX="70" RadiusY="70" />
                    </CombinedGeometry.Geometry1>
                    <CombinedGeometry.Geometry2>
                        <EllipseGeometry Center="220,120" RadiusX="70" RadiusY="70" />
                    </CombinedGeometry.Geometry2>
                </CombinedGeometry>
            </CombinedGeometry.Geometry2>
        </CombinedGeometry>
    </Path.Data>
</Path>

The resultant shape is shown below:

WPF Path with combined geometry containing three shapes

11 January 2015