title: WPF Binding to DependencyProperty of UserControl in DataTemplate is not working
body: <p>i have a custom userControl with DPs and have the problem that the binding to these properties only works if i use the controls outside of datatemplates. </p>
  
  <p>Outside of Datatemplates works the usercontrol great.</p>
  
  <p><strong>XAML to Test the UserControl in a DataTemplate</strong></p>
  
  <pre><code>        &lt;GroupBox Header="DataTemplate" Padding="5"&gt;
  
              &lt;ItemsControl ItemsSource="{Binding Dummies}"&gt;
                  &lt;ItemsControl.ItemTemplate&gt;
                      &lt;DataTemplate DataType="local:Dummy"&gt;
                          &lt;StackPanel Orientation="Horizontal" Margin="2"&gt;
  
                              &lt;common:QuarterPicker SelectedFirstDay="{Binding Gueltigkeit}" Margin="5,0" /&gt;
  
                              &lt;!--control the value of the item--&gt;
                              &lt;TextBlock Text="Gueltigkeit: "/&gt;
                              &lt;TextBlock Text="{Binding Gueltigkeit}"/&gt;
  
                          &lt;/StackPanel&gt;
                      &lt;/DataTemplate&gt;
                  &lt;/ItemsControl.ItemTemplate&gt;
  
              &lt;/ItemsControl&gt;
  
          &lt;/GroupBox&gt;
  </code></pre>
  
  <p><strong>MainWindow codebehind and ViewModel</strong></p>
  
  <pre><code>public partial class QuarterPickerTest : UserControl
  {
      public QuarterPickerTest()
      {
          InitializeComponent();
  
          this.DataContext = new ViewModel();
      }
  }
  
  public class ViewModel
  {
      public ViewModel()
      {
          this.Dummy = new Dummy { Gueltigkeit = DateTime.Today };
  
          this.Dummies = new List&lt;Dummy&gt;
              {
                  new Dummy {Gueltigkeit = DateTime.Today},
                  new Dummy {Gueltigkeit = DateTime.Today.AddMonths(6)},
                  new Dummy {Gueltigkeit = DateTime.Today.AddDays(-6)},
              };
      }
  
      public Dummy Dummy { get; set; }
  
      public List&lt;Dummy&gt; Dummies { get; set; }
  }
  </code></pre>
  
  <p><strong>Here is the code behind of my UserControl</strong></p>
  
  <pre><code>#region SelectedFirstDay
  
      public DateTime SelectedFirstDay
      {
          get { return (DateTime)GetValue(SelectedFirstDayProperty); }
          set { SetValue(SelectedFirstDayProperty, value); }
      }
  
      public static readonly DependencyProperty SelectedFirstDayProperty
          = DependencyProperty.Register("SelectedFirstDay", typeof (DateTime), typeof (QuarterPicker),
                                        new FrameworkPropertyMetadata(DateTime.Today, SelectedFirstDayPropertyChangedCallback) { BindsTwoWayByDefault = true });
  
      private static void SelectedFirstDayPropertyChangedCallback(DependencyObject dependencyObject, DependencyPropertyChangedEventArgs args)
      {
          var quarterPicker = dependencyObject as QuarterPicker;
          var date = (DateTime)args.NewValue;
          var quarter = GetQuarter(date);
  
          quarterPicker.UpdateProperties(date.Year, quarter);
  
          if (date.Year == quarterPicker.LeftInfiniteYear &amp;&amp; quarter == quarterPicker.LeftInfiniteQuarter)
              quarterPicker.ShowPopupButtonLeftInfinite();
      }
  
      #endregion
  </code></pre>
  
  <p>Thanks for any suggestions!</p>