title: WPF: Binding an integer to a TextBlock with TemplateBinding
body: <p>I have a custom control in WPF. In this I have a <code>DependencyProperty</code> of the type <code>int</code>. In the template for the custom control I have a <code>TextBlock</code>, I and would like to show the value of the integer in the <code>TextBlock</code>. But I can't get it to work.</p>
  
  <p>I'm using <code>TemplateBinding</code>. If I use the same code but change the type of the <code>DependencyProperty</code> to <code>string</code> it works fine. But I really want it to be an integer for the rest of my application to work.</p>
  
  <p>How can I do this?</p>
  
  <p>I've written simplified code that shows the problem. First the custom control:</p>
  
  <pre><code>public class MyCustomControl : Control
  {
      static MyCustomControl()
      {
          DefaultStyleKeyProperty.OverrideMetadata(typeof(MyCustomControl), new FrameworkPropertyMetadata(typeof(MyCustomControl)));
  
          MyIntegerProperty = DependencyProperty.Register("MyInteger", typeof(int), typeof(MyCustomControl), new FrameworkPropertyMetadata(0));
      }
  
  
      public int MyInteger
      {
          get
          {
              return (int)GetValue(MyCustomControl.MyIntegerProperty);
          }
          set
          {
              SetValue(MyCustomControl.MyIntegerProperty, value);
          }
      }
      public static readonly DependencyProperty MyIntegerProperty;
  }
  </code></pre>
  
  <p>And this is my default template:</p>
  
  <pre><code>&lt;Style TargetType="{x:Type local:MyCustomControl}"&gt;
      &lt;Setter Property="Template"&gt;
          &lt;Setter.Value&gt;
              &lt;ControlTemplate TargetType="{x:Type local:MyCustomControl}"&gt;
                  &lt;Border BorderThickness="1" CornerRadius="4" BorderBrush="Black" Background="Azure"&gt;
                      &lt;StackPanel Orientation="Vertical"&gt;
                          &lt;TextBlock Text="{TemplateBinding MyInteger}" HorizontalAlignment="Center" /&gt;
                      &lt;/StackPanel&gt;
                  &lt;/Border&gt;
              &lt;/ControlTemplate&gt;
          &lt;/Setter.Value&gt;
      &lt;/Setter&gt;
  &lt;/Style&gt;
  </code></pre>
  
  <p>And usage:</p>
  
  <pre><code>&lt;Window x:Class="CustomControlBinding.MainWindow"
      xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
      xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
      xmlns:local="clr-namespace:CustomControlBinding"
      Title="MainWindow" Height="350" Width="525"&gt;
  &lt;Grid&gt;
      &lt;local:MyCustomControl Width="100" Height="100" MyInteger="456" /&gt;
  &lt;/Grid&gt;
  </code></pre>
  
  <p></p>
  
  <p>What am I doing wrong?</p>
  
  <p>Thanks // David</p>