Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

like image 968
Drew Noakes Avatar asked Oct 09 '11 18:10

Drew Noakes


1 Answers

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>
like image 76
AnthonyWJones Avatar answered Sep 23 '22 10:09

AnthonyWJones