1
Vote

DateTimePicker not holding time data

description

When assigning the DateTimePicker a DateTime it is not holding the time data. After some testing with bindings and normal property sets, it seems that when setting DateTimeSelected on the DateTimePicker control it sets the Time value to the current Time after the set, negating whatever time you set it to.

comments

CT2049 wrote Apr 13, 2009 at 4:59 PM

An easy way to see this happening is to add the code:
DateTime myTime = new DateTime(2008, 12, 12, 18, 19, 20);
dateTimePicker.DateTimeSelected = myTime;

To DateTimePickerTest.xaml.cs's constructor

CT2049 wrote Apr 13, 2009 at 6:08 PM

I found the problem.

In DateTimePicker.cs the method: OnApplyTemplate()

has code that looks like this:

datePicker.SelectedDateChanged += delegate { SetCurrentDateTime(); };
timePicker.SelectedTimeChanged += delegate { SetCurrentDateTime(); };
datePicker.CurrentlySelectedDate = DateTimeSelected;
timePicker.SelectedTime = DateTimeSelected.TimeOfDay;

This needs to be changed to this:

datePicker.CurrentlySelectedDate = DateTimeSelected;
timePicker.SelectedTime = DateTimeSelected.TimeOfDay;
datePicker.SelectedDateChanged += delegate { SetCurrentDateTime(); };
timePicker.SelectedTimeChanged += delegate { SetCurrentDateTime(); };

Because the event listeners were set before the Value were when the values were changed it would change CurrentDateTime. Because the CurrentlySelectedDate was being selected and DatePicker uses the current time for it's time it would set CurrentDateTime's Time to the current time everytime. Reordering this code fixed the problem.

wrote Feb 14, 2013 at 7:48 PM