title: TemplateBindings in Custom Controls
body: <p>I'm just mucking about with custom controls in silverlight and for the life of me i can't get the TemplateBindings to work.  Can someone give this reduced version a once over to see if I'm missing something.</p>
  
  <p>So my ControlTemplate in the generic.xaml looks like </p>
  
  <pre><code>&lt;ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
      xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
      xmlns:local="clr-namespace:NumericStepperControl;assembly=NumericStepperControl"&gt;
      &lt;Style TargetType="local:NumericStepper"&gt;
          &lt;Setter Property="Template"&gt;
              &lt;Setter.Value&gt;
                  &lt;ControlTemplate TargetType="local:NumericStepper"&gt;
                      &lt;Grid&gt;
                          &lt;Grid.ColumnDefinitions&gt;
                              &lt;ColumnDefinition /&gt;
                              &lt;ColumnDefinition /&gt;
                          &lt;/Grid.ColumnDefinitions&gt;
  
                          &lt;Border Grid.Column="0" BorderBrush="Black" BorderThickness="2"  Width="50" Height="30"&gt;
                              &lt;TextBlock Width="50" Height="30" Text="{TemplateBinding Value}" /&gt;
                          &lt;/Border&gt;
                      &lt;/Grid&gt;
                  &lt;/ControlTemplate&gt;
              &lt;/Setter.Value&gt;        
          &lt;/Setter&gt;
      &lt;/Style&gt;
  &lt;/ResourceDictionary&gt;
  </code></pre>
  
  <p>and my control class looks like:</p>
  
  <pre><code>namespace NumericStepperControl
  {
      public class NumericStepper : Control
      {
          public static readonly DependencyProperty ValueProperty = DependencyProperty.Register("Value", typeof(int), typeof(NumericStepper), new PropertyMetadata(20));
  
          public NumericStepper()
              : base()
          {
              DefaultStyleKey = typeof( NumericStepper );
          }
  
          public int Value
          {
              get
              {
                  return (int)GetValue(ValueProperty);
              }
              set
              {
                  SetValue(ValueProperty, value);
              }
          }
      }
  }
  </code></pre>
  
  <p>I'm expecting when this runs the TextBlock will display the number 20. Any ideas as to why this isn't working?</p>
  
  <p>As a side not i have a separate project which contains a ref to the NumericStepperControl assembly and when it runs the controls seem to build correctly.</p>
  
  <p><strong>Edit... after a bit more investigation i have discovered that if i change the type of the Value property to a string that works fine.  Why does a text block not just call a toString on whatever is passed into it?  Is there a way round this as i can see it happening a lot?</strong></p>