Skip to content

Commit

Permalink
(GH-3746) Fix binding to Calendar SelectedDateProperty which breaks t…
Browse files Browse the repository at this point in the history
…ime selection and more...
  • Loading branch information
punker76 committed May 3, 2020
1 parent 0453652 commit c47d1b2
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 13 deletions.
10 changes: 9 additions & 1 deletion src/MahApps.Metro/Controls/TimePicker/DateTimePicker.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
using System.Windows;
using System.Windows.Controls;
using System.Windows.Controls.Primitives;
using System.Windows.Data;
using System.Windows.Input;
using System.Windows.Threading;

Expand Down Expand Up @@ -170,7 +171,7 @@ public override void OnApplyTemplate()
_calendar.SelectedDatesChanged += this.CalendarSelectedDateChanged;
_calendar.PreviewMouseUp += CalendarPreviewMouseUp;

_calendar.SetBinding(Calendar.SelectedDateProperty, GetBinding(SelectedDateTimeProperty));
_calendar.SetBinding(Calendar.SelectedDateProperty, GetBinding(SelectedDateTimeProperty, BindingMode.OneWay));
_calendar.SetBinding(Calendar.DisplayDateProperty, GetBinding(DisplayDateProperty));
_calendar.SetBinding(Calendar.DisplayDateStartProperty, GetBinding(DisplayDateStartProperty));
_calendar.SetBinding(Calendar.DisplayDateEndProperty, GetBinding(DisplayDateEndProperty));
Expand Down Expand Up @@ -366,6 +367,13 @@ protected override void ClockSelectedTimeChanged(object sender, SelectionChanged
//SetDatePartValues();
}

protected override void OnSelectedDateTimeChanged(DateTime? oldValue, DateTime? newValue)
{
this._calendar.SetCurrentValue(Calendar.SelectedDateProperty, newValue);

base.OnSelectedDateTimeChanged(oldValue, newValue);
}

private DateTime? GetSelectedDateTimeFromGUI()
{
// Because Calendar.SelectedDate is bound to this.SelectedDate return this.SelectedDate
Expand Down
26 changes: 14 additions & 12 deletions src/MahApps.Metro/Controls/TimePicker/TimePickerBase.cs
Original file line number Diff line number Diff line change
Expand Up @@ -485,6 +485,7 @@ public override void OnApplyTemplate()
protected virtual void ApplyCulture()
{
_deactivateRangeBaseEvent = true;

if (_ampmSwitcher != null)
{
_ampmSwitcher.Items.Clear();
Expand All @@ -509,14 +510,15 @@ protected virtual void ApplyCulture()
}

SetDefaultTimeOfDayValues();

_deactivateRangeBaseEvent = false;

WriteValueToTextBox();
}

protected Binding GetBinding(DependencyProperty property)
protected Binding GetBinding(DependencyProperty property, BindingMode bindingMode = BindingMode.Default)
{
return new Binding(property.Name) { Source = this };
return new Binding(property.Name) { Source = this, Mode = bindingMode };
}

protected virtual string GetValueForTextBox()
Expand Down Expand Up @@ -741,10 +743,12 @@ private void TimePickerPreviewKeyDown(object sender, RoutedEventArgs e)

private void TimePickerSelectionChanged(object sender, SelectionChangedEventArgs e)
{
if (!_deactivateRangeBaseEvent)
if (_deactivateRangeBaseEvent)
{
this.ClockSelectedTimeChanged(sender, e);
return;
}

this.ClockSelectedTimeChanged(sender, e);
}

private static void OnCultureChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
Expand Down Expand Up @@ -816,18 +820,16 @@ 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.OnSelectedDateTimeChanged(e.OldValue as DateTime?, e.NewValue as DateTime?);

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

timePartPickerBase.RaiseSelectedDateTimeChangedEvent(e.OldValue as DateTime?, e.NewValue as DateTime?);
}

timePartPickerBase.WriteValueToTextBox();
protected virtual void OnSelectedDateTimeChanged(DateTime? oldValue, DateTime? newValue)
{
this.SetHourPartValues(newValue.GetValueOrDefault().TimeOfDay);
}

private static void SetVisibility(UIElement partHours, UIElement partMinutes, UIElement partSeconds, TimePartVisibility visibility)
Expand Down

0 comments on commit c47d1b2

Please sign in to comment.