Draw special background - wpf

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.

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>

Related

Wpf canvas grid with coordinates

I have following canvas Code:
<Canvas>
<Canvas.Background>
<DrawingBrush TileMode="Tile" Viewport="0,0,40,40"
ViewportUnits="Absolute">
<DrawingBrush.Drawing>
<GeometryDrawing>
<GeometryDrawing.Geometry>
<RectangleGeometry Rect="0,0,400,400"/>
</GeometryDrawing.Geometry>
<GeometryDrawing.Pen>
<Pen Brush="Red" Thickness="1"/>
</GeometryDrawing.Pen>
</GeometryDrawing>
</DrawingBrush.Drawing>
</DrawingBrush>
</Canvas.Background>
</Canvas>
This canvas generates a grid like you can find it on excel.Now I want to know if ist possible to generate the coordinates too. With cooridnates I mean that the first top row in the first fiels has a tiny text in it that says "A0" and the next one has "B0" and so every cell has coordinates like Excel.Would this be possible?

Fixing blurred lines that are drawn by DrawingBrush

This is my drawing brush:
<DrawingBrush x:Key="GridBrush" TileMode="Tile"
Viewport="0,0,60,30" ViewportUnits="Absolute"
Viewbox="0,0,60,30" ViewboxUnits="Absolute">
<DrawingBrush.Drawing>
<GeometryDrawing>
<GeometryDrawing.Geometry>
<GeometryGroup>
<LineGeometry EndPoint="0,30"/>
<LineGeometry EndPoint="60,0"/>
</GeometryGroup>
</GeometryDrawing.Geometry>
<GeometryDrawing.Pen>
<Pen Brush="DarkGray" Thickness="1" DashCap="Flat" >
<Pen.DashStyle>
<DashStyle Dashes="2,2"/>
</Pen.DashStyle>
</Pen>
</GeometryDrawing.Pen>
</GeometryDrawing>
</DrawingBrush.Drawing>
</DrawingBrush>
This is where I use it:
<Border Width="721" Background="{StaticResource GridBrush}" UseLayoutRounding="True" SnapsToDevicePixels="True" >
And the effects is:
Basically the middle line is "okish" the other two are wider then they should. I tried using UseLayoutRounding, SnapsToDevicePixels but with or without those options nothing changes. I ran out of ideas by now :/

DrawingBrush to WPF DataGrid cells as Background

I'd like to set a DrawingBrush to DataGridCelland I use
<DrawingBrush TileMode="Tile" ViewportUnits="RelativeToBoundingBox" Viewport="0,0,0.05,1">
<DrawingBrush.Drawing>
<DrawingGroup>
<GeometryDrawing>
<GeometryDrawing.Pen>
<Pen Brush="Gray" Thickness="0.05"/>
</GeometryDrawing.Pen>
<GeometryDrawing.Geometry>
<LineGeometry StartPoint="0,1" EndPoint="1,0" />
</GeometryDrawing.Geometry>
</GeometryDrawing>
</DrawingGroup>
</DrawingBrush.Drawing>
</DrawingBrush>
I have undesired result when DataGridCells have unequal widths like below.
How should I change the Brush so that regardles of the widht of the individual cells the result would look like this.
Change ViewportUnits from RelativeToBoundingBox to Absolute and adjust Viewport.
The coordinate system is not relative to a bounding box. Values are interpreted directly in local space.
Something like this:
<DrawingBrush
x:Key="DrawingBrush"
TileMode="Tile"
ViewportUnits="Absolute"
Viewport="0,0,5,15">
which looks like this

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?
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.

WPF Drawing Paradox

How is it possible, that 2 ellipses with the same Radius where not (visually) with the same Radius?
in the image bellow, Black and Red ellipses has the same RadiusX... but look on the picture!
<GeometryDrawing Brush="Red">
<GeometryDrawing.Pen>
<Pen Brush="Yellow" Thickness="1"/>
</GeometryDrawing.Pen>
<GeometryDrawing.Geometry>
<GeometryGroup>
<EllipseGeometry x:Name="MediumCircle"
Center="0,0" RadiusX="4" RadiusY="4" />
</GeometryGroup>
</GeometryDrawing.Geometry>
</GeometryDrawing>
<GeometryDrawing Brush="Black">
<GeometryDrawing.Geometry>
<GeometryGroup>
<EllipseGeometry x:Name="SmallCircle"
Center="0,0" RadiusX="4" RadiusY="2"/>
</GeometryGroup>
</GeometryDrawing.Geometry>
</GeometryDrawing>
One has a stroked border and the other doesn't. You'll notice the radius lines up with the center of the stroked border.
Looks like the thickness is to blame. Make them the same for both.
I think I found the reply, but not sure... The Thickness of the ellipse goes not totally around the ellipse. If we Set the Thickness to 4 we will see only 2 pixels of ellipse, if we set at 8, we will not see the red circle anymore...

Resources