title: Is template binding to a Geometry possible
body: <p>I currently have a custom <code>Button</code> control which displays a <code>DrawingImage</code> set on the control via a dependency property of type <code>ControlTemplate</code>.  I have been attempting to move the <code>DrawingImage</code> into the control template and bind to a <code>Geometry</code> instead but have failed.  Is it possible to bind to a <code>Geometry</code> object?  If not why not, and if it is, what am I missing?</p>
  
  <p>The screenshot below shows my successful attempt on the right and the failed attempt on the left.</p>
  
  <p><img src="https://i.stack.imgur.com/oPEVj.png" alt="The problem"></p>
  
  <p>Example of binding to a DrawingImage (which works) - generic.xaml:</p>
  
  <pre><code>&lt;Style TargetType="{x:Type local:RoundButton3}"&gt;
      &lt;Setter Property="Template"&gt;
          &lt;Setter.Value&gt;
              &lt;ControlTemplate TargetType="{x:Type local:RoundButton3}"&gt;
                  &lt;Grid&gt;
                      &lt;Grid.ColumnDefinitions&gt;
                          &lt;ColumnDefinition Width="auto"/&gt;
                          &lt;ColumnDefinition Width="*"/&gt;
                      &lt;/Grid.ColumnDefinitions&gt;
  
                      &lt;Ellipse StrokeThickness="5" Stroke="Green"/&gt;
  
                      &lt;Viewbox Margin="10" Stretch="Uniform"&gt;
                          &lt;ContentControl 
                              Template="{TemplateBinding ImageTemplate}" /&gt;
                      &lt;/Viewbox&gt;
  
                      &lt;TextBlock VerticalAlignment="Center" 
                                 Grid.Column="1" 
                                 Text="Round Button" Margin="5,0,0,0" /&gt;
                  &lt;/Grid&gt;
              &lt;/ControlTemplate&gt;
          &lt;/Setter.Value&gt;
      &lt;/Setter&gt;
  &lt;/Style&gt;
  </code></pre>
  
  <p>with</p>
  
  <pre><code>public class RoundButton3 : Button
  {
      static RoundButton3()
      {
          DefaultStyleKeyProperty.OverrideMetadata(
              typeof(RoundButton3), 
              new FrameworkPropertyMetadata(typeof(RoundButton3)));
      }
  
      public static readonly DependencyProperty ImageTemplateProperty =
          DependencyProperty.Register(
              "ImageTemplate", typeof (ControlTemplate), typeof (RoundButton3),
              new FrameworkPropertyMetadata(
                  default(ControlTemplate),
                  FrameworkPropertyMetadataOptions.AffectsRender |
                  FrameworkPropertyMetadataOptions.AffectsMeasure));
  
      public ControlTemplate ImageTemplate
      {
          get { return (ControlTemplate)GetValue(ImageTemplateProperty); }
          set { SetValue(ImageTemplateProperty, value); }
      }
  }
  </code></pre>
  
  <p>and used like this</p>
  
  <pre><code>    &lt;Grid.Resources&gt;
          &lt;GeometryGroup x:Key="AddGeometry"&gt;
              &lt;LineGeometry StartPoint="25,12" EndPoint="25,38" /&gt;
              &lt;LineGeometry StartPoint="12,25" EndPoint="38,25" /&gt;
          &lt;/GeometryGroup&gt;
  
          &lt;ControlTemplate x:Key="RoundAddTemplate"&gt;
              &lt;Image SnapsToDevicePixels="True"&gt;
                  &lt;Image.Source&gt;
                      &lt;DrawingImage&gt;
                          &lt;DrawingImage.Drawing&gt;
                              &lt;DrawingGroup&gt;
                                  &lt;GeometryDrawing Pen="{StaticResource RoundButtonPen}" Geometry="{StaticResource AddGeometry}"/&gt;
                              &lt;/DrawingGroup&gt;
                          &lt;/DrawingImage.Drawing&gt;
                      &lt;/DrawingImage&gt;
                  &lt;/Image.Source&gt;
              &lt;/Image&gt;
          &lt;/ControlTemplate&gt;
      &lt;/Grid.Resources&gt;
  
     &lt;local:RoundButton3 
          Grid.Column="1"
          Content="RoundButton3" ImageTemplate="{StaticResource RoundAddTemplate}"/&gt;
  </code></pre>
  
  <p>My attempt at binding to a geometry resource (which doesn't work) - generic.xaml:</p>
  
  <pre><code>&lt;Style TargetType="{x:Type local:RoundButton2}"&gt;
      &lt;Setter Property="Template"&gt;
          &lt;Setter.Value&gt;
              &lt;ControlTemplate TargetType="{x:Type local:RoundButton2}"&gt;
                      ...
  
                      &lt;Viewbox Margin="10" Stretch="Uniform"&gt;
                          &lt;Image&gt;
                              &lt;Image.Source&gt;
                                  &lt;DrawingImage&gt;
                                      &lt;DrawingImage.Drawing&gt;
                                          &lt;DrawingGroup&gt;
                                              &lt;DrawingGroup.Children&gt;
                                                  &lt;GeometryDrawing 
                                                    Pen="{StaticResource RoundButtonPen}" 
                                                    Geometry="{TemplateBinding Geometry}" /&gt;
                                              &lt;/DrawingGroup.Children&gt;
                                          &lt;/DrawingGroup&gt;
                                      &lt;/DrawingImage.Drawing&gt;
                                  &lt;/DrawingImage&gt;
                              &lt;/Image.Source&gt;
                          &lt;/Image&gt;
                      &lt;/Viewbox&gt;
  
                      ...
                  &lt;/Grid&gt;
              &lt;/ControlTemplate&gt;
          &lt;/Setter.Value&gt;
      &lt;/Setter&gt;
  &lt;/Style&gt;
  </code></pre>
  
  <p>where </p>
  
  <pre><code>public class RoundButton2 : Button
  {
      static RoundButton2()
      {
          DefaultStyleKeyProperty.OverrideMetadata(
              typeof(RoundButton2), 
              new FrameworkPropertyMetadata(typeof(RoundButton2)));
      }
  
      #region Geometry
  
      public static readonly DependencyProperty GeometryProperty =
          DependencyProperty.Register(
              "Geometry", typeof (Geometry), typeof (RoundButton2),
              new FrameworkPropertyMetadata(
                  default(Geometry),
                  FrameworkPropertyMetadataOptions.AffectsRender | 
                  FrameworkPropertyMetadataOptions.AffectsMeasure));
  
      public Geometry Geometry
      {
          get { return (Geometry)GetValue(GeometryProperty); }
          set { SetValue(GeometryProperty, value); }
      }
  
      #endregion
  }
  </code></pre>
  
  <p>and used like</p>
  
  <pre><code>    &lt;local:RoundButton2 
          Grid.Column="0"
          Geometry="{StaticResource AddGeometry}"
          Content="RoundButton2" /&gt;
  </code></pre>
  
  <p>using the AddGeometry resource defined earlier.</p>