title: Custom Control Dependency Property Binding
body: <p>I am going insane trying to get this to work with even the most basic example. I cannot for the life of me get binding to work. Here is a super easy example that is not working for me. I MUST be doing something incorrect.</p>
  
  <p>My Custom Control in my control library assembly:</p>
  
  <pre><code>public class TestControl : Control
  {
      public static readonly DependencyProperty TestPropProperty =
          DependencyProperty.Register("TestProp", typeof(string), typeof(TestControl), new UIPropertyMetadata(null));
  
      public string TestProp
      {
          get { return (string)GetValue(TestPropProperty); }
          set { SetValue(TestPropProperty, value); }
      }
  
      static TestControl()
      {
          DefaultStyleKeyProperty.OverrideMetadata(typeof(TestControl), new FrameworkPropertyMetadata(typeof(TestControl)));
      }
  }
  </code></pre>
  
  <p>And its XAML template:</p>
  
  <pre><code>&lt;Style TargetType="{x:Type local:TestControl}"&gt;
      &lt;Setter Property="Template"&gt;
          &lt;Setter.Value&gt;
              &lt;ControlTemplate TargetType="{x:Type local:TestControl}"&gt;
                  &lt;Border Background="{TemplateBinding Background}" BorderBrush="{TemplateBinding BorderBrush}" BorderThickness="{TemplateBinding BorderThickness}"&gt;
                      &lt;StackPanel&gt;
                          &lt;TextBlock Text="Testing..." /&gt;
                          &lt;Label Content="{Binding TestProp}" Padding="10" /&gt;
                      &lt;/StackPanel&gt;
                  &lt;/Border&gt;
              &lt;/ControlTemplate&gt;
          &lt;/Setter.Value&gt;
      &lt;/Setter&gt;
  &lt;/Style&gt;
  </code></pre>
  
  <p>Here's the XAML consuming the control in a wpf window with a reference to my control library:</p>
  
  <pre><code>&lt;Grid&gt;
      &lt;ItemsControl Name="mylist"&gt;
          &lt;ItemsControl.ItemTemplate&gt;
              &lt;DataTemplate&gt;
                  &lt;my:TestControl TestProp="{Binding Path=Name}" /&gt;
              &lt;/DataTemplate&gt;
          &lt;/ItemsControl.ItemTemplate&gt;
      &lt;/ItemsControl&gt;
  &lt;/Grid&gt;
  </code></pre>
  
  <p>And here's the code behind:</p>
  
  <pre><code>public partial class Test2 : Window
  {
      public class TestObject : INotifyPropertyChanged
      {
          public event PropertyChangedEventHandler PropertyChanged;
          protected void OnPropertyChanged(string PropertyName)
          {
              if (PropertyChanged != null)
                  PropertyChanged(this, new PropertyChangedEventArgs(PropertyName));
          }
  
          private int _id;
          public int id
          {
              get { return _id; }
              set { _id = value; OnPropertyChanged("id"); }
          }
  
          private string _Name;
          public string Name
          {
              get { return _Name; }
              set { _Name = value; OnPropertyChanged("Name"); }
          }
      }
  
      public Test2()
      {
          InitializeComponent();
  
          mylist.ItemsSource = new TestObject[]
          {
              new TestObject(){ id = 1, Name = "Tedd" },
              new TestObject(){ id = 2, Name = "Fred" },
              new TestObject(){ id = 3, Name = "Jim" },
              new TestObject(){ id = 4, Name = "Jack" },
          };
      }
  }
  </code></pre>
  
  <p>Running this example gives me four instances of the control, however I only see the "Testing..." TextBlock on each. My label is never bound. What am I misunderstanding and doing incorrectly?</p>