EFanZh
EFanZh

Reputation: 2436

How to create a Brush with a background in WPF?

I want to create a brush that draws a ellipse on the top-right corner, I tried this:

<DrawingBrush Stretch="None" AlignmentX="Right" AlignmentY="Top">
    <DrawingBrush.Drawing>
        <GeometryDrawing Brush="Green">
            <GeometryDrawing.Geometry>
                <EllipseGeometry RadiusX="60.0" RadiusY="60.0" />
            </GeometryDrawing.Geometry>
        </GeometryDrawing>
    </DrawingBrush.Drawing>
</DrawingBrush>

The ellipse is in its position, but the rest area is transparent. Can I create a brush that draws an ellipse on the top-right corner with a non-transparent background? Can I use another brush as background?

Upvotes: 0

Views: 2119

Answers (1)

Jay
Jay

Reputation: 57959

Your DrawingBrush can contain a DrawingGroup with multiple drawings, each with its own brush. Here I have added a pale green RectangleGeometry that fits behind your ellipse and serves as the background:

    <DrawingBrush Stretch="None" AlignmentX="Right" AlignmentY="Top">
        <DrawingBrush.Drawing>
            <DrawingGroup>
                <GeometryDrawing Brush="PaleGreen">
                    <GeometryDrawing.Geometry>
                        <RectangleGeometry Rect="-60,-60,120,120" />
                    </GeometryDrawing.Geometry>
                </GeometryDrawing>
                <GeometryDrawing Brush="Green">
                    <GeometryDrawing.Geometry>
                        <EllipseGeometry RadiusX="60.0" RadiusY="60.0" />
                    </GeometryDrawing.Geometry>
                </GeometryDrawing>
            </DrawingGroup>
        </DrawingBrush.Drawing>
    </DrawingBrush>

Given the requirement to have the background fill all available space, I recommend giving the Window.Background the fill brush and then overlaying another element in the upper right corner to contain the ellipse.

Upvotes: 2

Related Questions