title: Dynamic Brushes in XAML
body: <p>I have a definition for a data template that looks as follows:</p>
  
  <pre><code>&lt;DataTemplate DataType="{x:Type HeatMap:BlockItem}"&gt;
          &lt;Grid  Visibility="{Binding IsVisible}"&gt;
              &lt;Border Name="BlockBorder" Width="{Binding Width}" Height="{Binding Height}"&gt;
                  &lt;Border.Background&gt;
                      &lt;LinearGradientBrush EndPoint="0.5,1" StartPoint="0.5,0"&gt;
                          &lt;GradientStop Color="{Binding Colour}" Offset="1"/&gt;
                          &lt;GradientStop Color="White"/&gt;
                      &lt;/LinearGradientBrush&gt;
                  &lt;/Border.Background&gt;
              &lt;/Border&gt;
          &lt;/Grid&gt;
      &lt;/DataTemplate&gt;
  </code></pre>
  
  <p>As can be seen, <code>BlockItem</code> has a property of type <code>Color</code> called <code>Colour</code> which is bound to the first color of the <code>LinearGradientBrush</code> which fills a border, making it look like a filled rectangle.</p>
  
  <p>Now I don't always want Linear Gradient Brushes to style the fill of this rectangle. Some rectangles on my canvas may need to be filled with SolidBrushes, for example. I considered creating a <code>Brush</code> property on the <code>BlockItem</code> class instead of a <code>Color</code> property and binding the entire <code>Border.Background</code> to that, but there are 2 problems with this: </p>
  
  <ul>
  <li>I don't know how the XAML should look to specify a binding to the entire object <code>Background</code> property.</li>
  <li>In the code where I create <code>BlockItem</code>s, if  I instantiate a new Brush for every single BlockItem (bear in mind, there maybe be many drawn on a canvas at a time), will this not make it really inefficient and slow?</li>
  </ul>