Skip to content

Commit

Permalink
(MahAppsGH-3746) Introduce ClockSelectedTimeChanged and CalendarSelec…
Browse files Browse the repository at this point in the history
…tedDateChanged
  • Loading branch information
punker76 committed Apr 29, 2020
1 parent 65cbcd5 commit d6e81f8
Show file tree
Hide file tree
Showing 3 changed files with 58 additions and 24 deletions.
61 changes: 46 additions & 15 deletions src/MahApps.Metro/Controls/TimePicker/DateTimePicker.cs
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,11 @@ static DateTimePicker()
IsClockVisibleProperty.OverrideMetadata(typeof(DateTimePicker), new PropertyMetadata(OnClockVisibilityChanged));
}

public DateTimePicker()
{
this.SetCurrentValue(DisplayDateProperty, DateTime.Today);
}

/// <inheritdoc />
protected override void FocusElementAfterIsDropDownOpenChanged()
{
Expand Down Expand Up @@ -151,7 +156,7 @@ public override void OnApplyTemplate()
{
_calendar.PreviewKeyDown -= this.CalendarPreviewKeyDown;
_calendar.DisplayDateChanged -= this.CalendarDisplayDateChanged;
_calendar.SelectedDatesChanged -= CalendarSelectedDateChanged;
_calendar.SelectedDatesChanged -= this.CalendarSelectedDateChanged;
_calendar.PreviewMouseUp -= CalendarPreviewMouseUp;
}

Expand All @@ -163,7 +168,7 @@ public override void OnApplyTemplate()
{
_calendar.PreviewKeyDown += this.CalendarPreviewKeyDown;
_calendar.DisplayDateChanged += this.CalendarDisplayDateChanged;
_calendar.SelectedDatesChanged += CalendarSelectedDateChanged;
_calendar.SelectedDatesChanged += this.CalendarSelectedDateChanged;
_calendar.PreviewMouseUp += CalendarPreviewMouseUp;

_calendar.SetBinding(Calendar.SelectedDateProperty, GetBinding(SelectedDateTimeProperty));
Expand All @@ -175,7 +180,7 @@ public override void OnApplyTemplate()
_calendar.SetBinding(FlowDirectionProperty, GetBinding(FlowDirectionProperty));
}

SetDatePartValues();
//SetDatePartValues();
}

private static void CalendarPreviewMouseUp(object sender, MouseButtonEventArgs e)
Expand Down Expand Up @@ -257,13 +262,6 @@ protected override string GetValueForTextBox()
return valueForTextBox;
}

protected override void OnRangeBaseValueChanged(object sender, SelectionChangedEventArgs e)
{
base.OnRangeBaseValueChanged(sender, e);

SetDatePartValues();
}

protected override void OnTextBoxLostFocus(object sender, RoutedEventArgs e)
{
if (!(sender is DatePickerTextBox textBox))
Expand Down Expand Up @@ -311,14 +309,36 @@ private static void OnClockVisibilityChanged(DependencyObject d, DependencyPrope
d.CoerceValue(OrientationProperty);
}

private static void CalendarSelectedDateChanged(object sender, SelectionChangedEventArgs e)
private void CalendarSelectedDateChanged(object sender, SelectionChangedEventArgs e)
{
if (e.AddedItems.Count > 0 && this.SelectedDateTime.HasValue && DateTime.Compare((DateTime)e.AddedItems[0], this.SelectedDateTime.Value) != 0)
{
this.SetCurrentValue(SelectedDateTimeProperty, (DateTime?)e.AddedItems[0] + this.GetSelectedTimeFromGUI());
}
else
{
if (e.AddedItems.Count == 0)
{
this.SetCurrentValue(SelectedDateTimeProperty, (DateTime?)null);
return;
}

if (!this.SelectedDateTime.HasValue)
{
if (e.AddedItems.Count > 0)
{
this.SetCurrentValue(SelectedDateTimeProperty, (DateTime?)e.AddedItems[0] + this.GetSelectedTimeFromGUI());
}
}
}

/*
var dateTimePicker = (DateTimePicker)((Calendar)sender).TemplatedParent;
/* Without deactivating changing SelectedTime would callbase.OnSelectedTimeChanged.
* This would write too and this would result in duplicate writing.
* More problematic would be instead that a short amount of time SelectedTime would be as value in TextBox
*/
// Without deactivating changing SelectedTime would callbase.OnSelectedTimeChanged.
// This would write too and this would result in duplicate writing.
// More problematic would be instead that a short amount of time SelectedTime would be as value in TextBox
dateTimePicker._deactivateWriteValueToTextBox = true;
var dt = e.AddedItems.Count > 0 ? (DateTime?)e.AddedItems[0] : default;
Expand All @@ -334,6 +354,17 @@ private static void CalendarSelectedDateChanged(object sender, SelectionChangedE
dateTimePicker._deactivateWriteValueToTextBox = false;
dateTimePicker.WriteValueToTextBox();
*/
}

protected override void ClockSelectedTimeChanged(object sender, SelectionChangedEventArgs e)
{
var time = this.GetSelectedTimeFromGUI() ?? TimeSpan.Zero;
var date = SelectedDateTime ?? DateTime.Today;

this.SetCurrentValue(SelectedDateTimeProperty, date.Date + time);

//SetDatePartValues();
}

private DateTime? GetSelectedDateTimeFromGUI()
Expand Down
9 changes: 9 additions & 0 deletions src/MahApps.Metro/Controls/TimePicker/TimePicker.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
using System;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Controls.Primitives;
using System.Windows.Threading;

Expand Down Expand Up @@ -40,6 +41,14 @@ protected override void FocusElementAfterIsDropDownOpenChanged()
});
}

protected override void ClockSelectedTimeChanged(object sender, SelectionChangedEventArgs e)
{
var time = this.GetSelectedTimeFromGUI() ?? TimeSpan.Zero;
var date = SelectedDateTime ?? DateTime.Today;

this.SetCurrentValue(SelectedDateTimeProperty, date.Date + time);
}

protected override void OnTextBoxLostFocus(object sender, RoutedEventArgs e)
{
if (!(sender is DatePickerTextBox textBox))
Expand Down
12 changes: 3 additions & 9 deletions src/MahApps.Metro/Controls/TimePicker/TimePickerBase.cs
Original file line number Diff line number Diff line change
Expand Up @@ -519,13 +519,7 @@ protected virtual string GetValueForTextBox()

protected abstract void OnTextBoxLostFocus(object sender, RoutedEventArgs e);

protected virtual void OnRangeBaseValueChanged(object sender, SelectionChangedEventArgs e)
{
// 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 abstract void ClockSelectedTimeChanged(object sender, SelectionChangedEventArgs e);

protected virtual void OnSelectedTimeChanged(TimePickerBaseSelectionChangedEventArgs<DateTime?> e)
{
Expand Down Expand Up @@ -739,7 +733,7 @@ private void TimePickerSelectionChanged(object sender, SelectionChangedEventArgs
{
if (!_deactivateRangeBaseEvent)
{
OnRangeBaseValueChanged(sender, e);
this.ClockSelectedTimeChanged(sender, e);
}
}

Expand Down Expand Up @@ -876,7 +870,7 @@ private static void SetDefaultTimeOfDayValue(Selector selector)
return new TimeSpan(hours, minutes, seconds);
}

return this.SelectedDateTime.GetValueOrDefault().TimeOfDay;
return null; //this.SelectedDateTime.GetValueOrDefault().TimeOfDay;
}
}

Expand Down

0 comments on commit d6e81f8

Please sign in to comment.