0

Is it possible in WPF to draw the background of a window the following way:

  • the background of the window is for instance white
  • vertical colored lines of 20 pixels width with a same-size gap in between, so that you get a red-striped pattern
  • horizontal colored lines of 20 pixels height in the same color, also with a same-size gap in between
  • the areas where the horizontal and vertical red lines overlap each other should be shown in white again, so that you get kind of a checkered pattern

I know how to draw lines in WPF and I know how to repeat a drawing to fill the whole background, but I don't know how the manage the part with the overlapping colors vanishing.

Nostromo
  • 1,177
  • 10
  • 28

1 Answers1

2

Not sure how large the gap is supposed to be. You may however adjust a DrawingBrush like this:

<Window.Background>
    <DrawingBrush TileMode="Tile" ViewportUnits="Absolute" Viewport="0,0,100,100">
        <DrawingBrush.Drawing>
            <DrawingGroup>
                <GeometryDrawing Brush="White">
                    <GeometryDrawing.Geometry>
                        <RectangleGeometry Rect="0,0,100,100"/>
                    </GeometryDrawing.Geometry>
                </GeometryDrawing>
                <GeometryDrawing
                    Geometry="M50,0 L50,40 M50,60 L50,100 M0,50 L40,50 M60,50 L100,50">
                    <GeometryDrawing.Pen>
                        <Pen Brush="Red" Thickness="20"/>
                    </GeometryDrawing.Pen>
                </GeometryDrawing>
            </DrawingGroup>
        </DrawingBrush.Drawing>
    </DrawingBrush>
</Window.Background>

Or with another TileMode:

<Window.Background>
    <DrawingBrush TileMode="FlipXY" ViewportUnits="Absolute" Viewport="0,0,50,50">
        <DrawingBrush.Drawing>
            <DrawingGroup>
                <GeometryDrawing Brush="White">
                    <GeometryDrawing.Geometry>
                        <RectangleGeometry Rect="0,0,50,50"/>
                    </GeometryDrawing.Geometry>
                </GeometryDrawing>
                <GeometryDrawing Geometry="M0,45 L40,45 M45,0 L45,40">
                    <GeometryDrawing.Pen>
                        <Pen Brush="Red" Thickness="10"/>
                    </GeometryDrawing.Pen>
                </GeometryDrawing>
            </DrawingGroup>
        </DrawingBrush.Drawing>
    </DrawingBrush>
</Window.Background>
Clemens
  • 123,504
  • 12
  • 155
  • 268
  • What's the `RectangleGeometry` for? At least in the first version I can remove it without changing the resulting background? – Nostromo Oct 08 '18 at 15:38
  • It's the white background. Without it, the background would be transparent. – Clemens Oct 08 '18 at 16:57