title: Implementing two-way binding in WPF with a DateTime DependencyProperty
body: <p>I've built a WPF user control that contains, among other controls, a DatePicker.  My application is written with the MVVM design pattern.  I want to be able to insert my user control into a view and bind the user control's DatePicker's SelectedDate property to a property of DateTime on my view's ViewModel.  I want the default value shown to be the value stored on my view's viewmodel, and I want changes to the date through interaction with the DatePicker to update my view's viewmodel DateTime property.</p>
  
  <p>I'm successfully seeing the proper bound value displayed in the DatePicker control, but when I change the date my view's viewmodel's DateTime property setter is not firing.</p>
  
  <p>This is what I have for my user control's code-behind:</p>
  
  <pre><code>public partial class AgeDiscountUserControl : UserControl
  {
      public AgeDiscountUserControl()
      {
          InitializeComponent();
      }
  
      public DateTime DateTime
      {
          get { return (DateTime)GetValue(DateTimeProperty); }
          set { SetValue(DateTimeProperty, value); }
      }
  
      public static readonly DependencyProperty DateTimeProperty =
          DependencyProperty.Register("DateTime", typeof(DateTime), typeof(AgeDiscountUserControl));
  }
  </code></pre>
  
  <p>And in my user control's XAML, I have:</p>
  
  <pre><code>...
  xmlns:my="clr-namespace:jkshay.UserControls"
  ...
  &lt;DatePicker Grid.Row="0" SelectedDate="{Binding DateTime, RelativeSource={RelativeSource FindAncestor, AncestorLevel=1, AncestorType=my:AgeDiscountUserControl}}" DockPanel.Dock="Right"/&gt;
  ...
  </code></pre>
  
  <p>In my view's viewmodel (which implements INotifyPropertyChanged), I've a DateTime property to which I'd like to bind my user control's DatePicker's SelectedDate property.</p>
  
  <pre><code>...
  private DateTime birthdate;
  public DateTime Birthdate
  {
      get { return birthdate; }    
      set
      {
          if(birthdate != value)
          {  
              birthdate = value;
              NotifyPropertyChanged(() =&gt; Birthdate);
          }
      }
  }
  ...
  </code></pre>
  
  <p>Finally, in my view's XAML, my AgeDiscountUserControl's binding is:</p>
  
  <pre><code>...
  &lt;uc:AgeDiscountUserControl DateTime="{Binding Birthdate}"/&gt;
  ...
  </code></pre>
  
  <p>As stated previously, the proper value is initially displayed in the user control's DatePicker, but changes made by the DatePicker don't affect the bound property.</p>
  
  <p>Am I missing something here, or do I simply have a complete misunderstanding of DependencyProperties?</p>
  
  <p>I should mention that if I insert a DatePicker in my view and bind it to my viewmodel's Birthdate property, it works as expected.</p>