Search code examples
.netsilverlightgeometryclippingclip

How to invert clipping geometry in Silverlight/WPF?


The UIElement.Clip property takes a Geometry object and uses it to clip away the outside of the UIElement. I would like to do the geometric inverse and punch a hole into the element instead.

Anyone know how to do this?

I imagine creating an inverted version of the clip geometry would work, but I can't find a way to do this.


EDIT It seems that WPF has Geometry.Combine which can be used to subtract one geometry from another, though this isn't available in Silverlight. If it were, I could subtract the clip geometry from the rectangle of the element's bounding rectangle, and use that to clip instead.


Solution

  • One approach in Silverlight is to use a GeometryGroup and include in the group a very large rectangle starting at a distant negative position.

    For example the following blue square has smaller square hole:-

        <Rectangle Fill="Blue" HorizontalAlignment="Center" VerticalAlignment="Center" Height="200" Width="200">
            <Rectangle.Clip>
                <GeometryGroup>
                    <RectangleGeometry Rect="-2048 -2048 4096 4096" />
                    <RectangleGeometry Rect="100 100 50 50" />
                </GeometryGroup>
            </Rectangle.Clip>
        </Rectangle>