WPF - How can I make a brush that paints graph-paper-like squares?

Drew Noakes picture Drew Noakes · Apr 30, 2009 · Viewed 11k times · Source

How might I create a brush that paints a regular, repeated grid of 1-unit thick lines spaced evenly in both the horizontal and vertical axes? Imagine graph paper, if you will.

Ideally the solution would allow control over the brushes used for the lines and the background (the regions within the squares). In this way the background could be transparent, so that the grid could serve as an overlay.

EDIT Here is an image that shows the result of Tom's answer below:

For this example a grid was used to composite three layers to show that the grid is truly transparent.

Answer

Thomas picture Thomas · Apr 30, 2009

from http://msdn.microsoft.com/en-us/library/aa480159.aspx

<DrawingBrush Viewport="0,0,10,10" 
              ViewportUnits="Absolute"
              TileMode="Tile">
  <DrawingBrush.Drawing>
    <DrawingGroup>
      <GeometryDrawing Geometry="M0,0 L1,0 1,0.1, 0,0.1Z" Brush="Green" />
      <GeometryDrawing Geometry="M0,0 L0,1 0.1,1, 0.1,0Z" Brush="Green" />
    </DrawingGroup>
  </DrawingBrush.Drawing>
</DrawingBrush>