Skip to content

Commit

Permalink
(MahAppsGH-3746) Fix input for date and time text input for DateTimeP…
Browse files Browse the repository at this point in the history
…icker

- if the Time is selected before a Date is selected use DateTime.Today instead of DateTime.Min
- If a new Value is selected from the Calendar we use the already Selected time.
- Implemented a new private `bool` to disable changing the time.
  • Loading branch information
punker76 committed Apr 28, 2020
1 parent 5873948 commit 716b839
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 1 deletion.
2 changes: 2 additions & 0 deletions src/MahApps.Metro/Controls/TimePicker/DateTimePicker.cs
Original file line number Diff line number Diff line change
Expand Up @@ -192,7 +192,9 @@ protected override void OnTextBoxLostFocus(object sender, RoutedEventArgs e)
DateTime ts;
if (DateTime.TryParse(((DatePickerTextBox)sender).Text, SpecificCultureInfo, System.Globalization.DateTimeStyles.None, out ts))
{
this._deactivateAdjustTimeOnDateChange = true;
this.SetCurrentValue(SelectedDateTimeProperty, ts);
this._deactivateAdjustTimeOnDateChange = false;
}
else
{
Expand Down
13 changes: 12 additions & 1 deletion src/MahApps.Metro/Controls/TimePicker/TimePickerBase.cs
Original file line number Diff line number Diff line change
Expand Up @@ -175,6 +175,7 @@ public abstract class TimePickerBase : Control
private UIElement _secondHand;
private Selector _secondInput;
protected DatePickerTextBox _textBox;
protected bool _deactivateAdjustTimeOnDateChange;

static TimePickerBase()
{
Expand Down Expand Up @@ -485,7 +486,10 @@ protected virtual string GetValueForTextBox()

protected virtual void OnRangeBaseValueChanged(object sender, SelectionChangedEventArgs e)
{
this.SetCurrentValue(SelectedDateTimeProperty, this.SelectedDateTime.GetValueOrDefault().Date + this.GetSelectedTimeFromGUI());
// If we do not have a date yet we should use Today instead of DateTime.Min
var newDateTime = SelectedDateTime ?? DateTime.Today;

this.SetCurrentValue(SelectedDateTimeProperty, newDateTime.Date + this.GetSelectedTimeFromGUI());
}

protected virtual void OnSelectedTimeChanged(TimePickerBaseSelectionChangedEventArgs<DateTime?> e)
Expand Down Expand Up @@ -690,6 +694,13 @@ private static void OnSelectedDateTimeChanged(DependencyObject d, DependencyProp
return;
}

// if just the DatePart changed, we should set the old TimePart for the NewValue
if (!timePartPickerBase._deactivateAdjustTimeOnDateChange && e.OldValue is DateTime oldVal && e.NewValue is DateTime newVal && oldVal.Date != newVal.Date && newVal.TimeOfDay.Ticks == 0 && oldVal.TimeOfDay.Ticks != 0)
{
timePartPickerBase.SetCurrentValue(SelectedDateTimeProperty, ((DateTime?)e.NewValue)?.Date + ((DateTime?)e.OldValue)?.TimeOfDay);
return;
}

timePartPickerBase.SetHourPartValues((e.NewValue as DateTime?).GetValueOrDefault().TimeOfDay);

timePartPickerBase.OnSelectedTimeChanged(new TimePickerBaseSelectionChangedEventArgs<DateTime?>(SelectedDateTimeChangedEvent, (DateTime?)e.OldValue, (DateTime?)e.NewValue));
Expand Down

0 comments on commit 716b839

Please sign in to comment.