-
Notifications
You must be signed in to change notification settings - Fork 2.5k
Description
Hi,
I’m using the DateTimePicker control and binding its SelectedDateTime property to a DateTime? property in my ViewModel.
Bug
If I pick the date first, the SelectedDateTime.Kind is Unspecified.
If I pick the time first, the Kind becomes Local.
Why it happens
Looking at the source:
CalendarSelectedDateChanged (inside the DateTimePicker.calendar part) returns a DateTime with Kind == Unspecified.
ClockSelectedTimeChanged (inside TimePickerBase) sets SelectedDateTime = DateTime.Today when the previous value is null; DateTime.Today has Kind == Local.
Why this is a problem
Consumers of the control have to handle two different DateTimeKind cases, which is easy to overlook and may introduce bugs.
Possible fix
Replace DateTime.Today with DateTimeOffset.Now.Date (or another DateTime that keeps Kind == Unspecified) so that SelectedDateTime.Kind is consistently Unspecified, no matter which part (date or time) the user selects first.
Environment
MahApps.Metro version: v2.4.10
Target Framework: .Net 8