title: DrawingBrush on Canvas not working
body: <p>The XAML that I have in my question was taken directly from <a href="https://stackoverflow.com/questions/6434284/how-to-draw-gridline-on-wpf-canvas">this</a> question on SO. It's simply XAML that creates a tiled DrawingBrush to render rectangles (squares in my case) against the background of a canvas. My problem is that this XAML only works when I set the Width and Height of the Canvas to anything other than "Auto". But when I set the Width and Height of canvasMain to Auto then no squares are drawn on the background. </p>
  
  <p><strong>How can I have my Width and Height set to "Auto" and also have the DrawingBrush render the squares on my canvas?</strong></p>
  
  <p>Here is my XAML:</p>
  
  <pre><code>&lt;Window.Resources&gt;
    &lt;DrawingBrush x:Key="GridTile" Stretch="None" TileMode="Tile"
      Viewport="0,0 20,20" ViewportUnits="Absolute"&gt;
      &lt;!-- ^^^^^^^^^^^ set the size of the tile--&gt;
      &lt;DrawingBrush.Drawing&gt;
        &lt;GeometryDrawing&gt;
          &lt;GeometryDrawing.Geometry&gt;
            &lt;!-- draw a single X --&gt;
            &lt;GeometryGroup&gt;
              &lt;!-- top-left to top-right --&gt;
              &lt;LineGeometry StartPoint="0,0" EndPoint="20,0" /&gt;
  
              &lt;!-- top-left to bottom-left --&gt;
              &lt;LineGeometry StartPoint="0,0" EndPoint="0,20" /&gt;
  
              &lt;!-- bottom-left to bottom-right --&gt;
              &lt;LineGeometry StartPoint="0,20" EndPoint="20,20" /&gt;
  
              &lt;!-- top-right to bottom-right --&gt;
              &lt;LineGeometry StartPoint="20,0" EndPoint="20,20" /&gt;
  
            &lt;/GeometryGroup&gt;
          &lt;/GeometryDrawing.Geometry&gt;
          &lt;GeometryDrawing.Pen&gt;
            &lt;!-- set color and thickness of lines --&gt;
            &lt;Pen Thickness="1" Brush="Silver" /&gt;
          &lt;/GeometryDrawing.Pen&gt;
        &lt;/GeometryDrawing&gt;
      &lt;/DrawingBrush.Drawing&gt;
    &lt;/DrawingBrush&gt;
    &lt;DrawingBrush x:Key="OffsetGrid" Stretch="None" AlignmentX="Left" AlignmentY="Top"&gt;
      &lt;DrawingBrush.Transform&gt;
        &lt;!-- set the left and top offsets --&gt;
        &lt;TranslateTransform X="0" Y="0" /&gt;
      &lt;/DrawingBrush.Transform&gt;
      &lt;DrawingBrush.Drawing&gt;
        &lt;GeometryDrawing Brush="{StaticResource GridTile}" &gt;
          &lt;GeometryDrawing.Geometry&gt;
            &lt;!-- set the width and height filled with the tile from the origin --&gt;
            &lt;RectangleGeometry Rect="0,0 160,160" /&gt;
          &lt;/GeometryDrawing.Geometry&gt;
        &lt;/GeometryDrawing&gt;
      &lt;/DrawingBrush.Drawing&gt;
    &lt;/DrawingBrush&gt;
  &lt;/Window.Resources&gt;
  
  &lt;Canvas Name="canvasMain" HorizontalAlignment="Left" Margin="0,0,0,0" VerticalAlignment="Top" Width="Auto" Height="Auto" Background="{StaticResource OffsetGrid}"&gt;&lt;/Canvas&gt;
  </code></pre>