Daily Archives: November 10, 2010

Updating Dates with WPF DatePicker

I spent hours and hours trying to find this piece of information. Finally I was very happy to have Julia Kornich, a technical writer on the EF team, save the day!

By default a DatePicker control does not notify it’s datasource of changes.

Even when you are doing a drag & drop application.

I had a DataGrid where every column but the date columns was pushing changes back to the database.

What was wrong with the DatePicker?

You need to explicitly tell it to do the job with the UpdateSourceTrigger attribute. It is not in the XAML by default. And in order for the notification to happen when the user changes the date, the UpdateSourceTrigger needs to be set to PropertyChanged.

<DatePicker
   SelectedDate="{Binding Path=OrderDate, Mode=TwoWay, 
                          ValidatesOnExceptions=true, 
                          NotifyOnValidationError=true, 
                          UpdateSourceTrigger=PropertyChanged}"

   DataContext="{Binding}"
/>

And there you have it!