Skip to content

Commit

Permalink
(MahAppsGH-3746) Set selected time from textbox
Browse files Browse the repository at this point in the history
  • Loading branch information
punker76 committed May 6, 2020
1 parent 966f01b commit 3e47365
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 51 deletions.
23 changes: 6 additions & 17 deletions src/MahApps.Metro/Controls/TimePicker/DateTimePicker.cs
Original file line number Diff line number Diff line change
Expand Up @@ -178,8 +178,6 @@ public override void OnApplyTemplate()
_calendar.SetBinding(Calendar.IsTodayHighlightedProperty, GetBinding(IsTodayHighlightedProperty));
_calendar.SetBinding(FlowDirectionProperty, GetBinding(FlowDirectionProperty));
}

//SetDatePartValues();
}

private static void CalendarPreviewMouseUp(object sender, MouseButtonEventArgs e)
Expand Down Expand Up @@ -241,7 +239,7 @@ private static void OnSelectedDateFormatChanged(DependencyObject d, DependencyPr
}
}

protected sealed override void ApplyCulture()
protected override void ApplyCulture()
{
base.ApplyCulture();

Expand All @@ -261,18 +259,18 @@ protected override string GetValueForTextBox()
return valueForTextBox;
}

protected override void OnTextBoxLostFocus(object sender, RoutedEventArgs e)
protected override void SetSelectedDateTime()
{
if (!(sender is DatePickerTextBox textBox))
if (this._textBox is null)
{
return;
}

if (DateTime.TryParse(textBox.Text, SpecificCultureInfo, System.Globalization.DateTimeStyles.None, out var dateTime))

if (DateTime.TryParse(_textBox.Text, SpecificCultureInfo, System.Globalization.DateTimeStyles.None, out var dateTime))
{
this._deactivateAdjustTimeOnDateChange = true;
this.SetCurrentValue(SelectedDateTimeProperty, dateTime);
this._deactivateAdjustTimeOnDateChange = false;
this.SetCurrentValue(DisplayDateProperty, dateTime);
}
else
{
Expand Down Expand Up @@ -324,14 +322,6 @@ private void CalendarSelectedDateChanged(object sender, SelectionChangedEventArg
}
}

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 OnSelectedDateTimeChanged(DateTime? oldValue, DateTime? newValue)
{
this._calendar.SetCurrentValue(Calendar.SelectedDateProperty, newValue);
Expand All @@ -343,7 +333,6 @@ protected override void OnSelectedDateTimeChanged(DateTime? oldValue, DateTime?
{
// Because Calendar.SelectedDate is bound to this.SelectedDate return this.SelectedDate
var selectedDate = SelectedDateTime;

if (selectedDate != null)
{
return selectedDate.Value.Date + GetSelectedTimeFromGUI().GetValueOrDefault();
Expand Down
25 changes: 6 additions & 19 deletions src/MahApps.Metro/Controls/TimePicker/TimePicker.cs
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
using System;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Controls.Primitives;
using System.Windows.Threading;

namespace MahApps.Metro.Controls
Expand Down Expand Up @@ -41,34 +39,23 @@ protected override void FocusElementAfterIsDropDownOpenChanged()
});
}

protected override void ClockSelectedTimeChanged(object sender, SelectionChangedEventArgs e)
protected override void SetSelectedDateTime()
{
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))
if (this._textBox is null)
{
return;
}

if (TimeSpan.TryParse(textBox.Text, SpecificCultureInfo, out var timeSpan))
if (TimeSpan.TryParse(_textBox.Text, SpecificCultureInfo, out var timeSpan))
{
this.SetCurrentValue(SelectedDateTimeProperty, this.SelectedDateTime.GetValueOrDefault().Date + timeSpan);
}
else
{
if (string.IsNullOrEmpty(textBox.Text))
{
this.SetCurrentValue(SelectedDateTimeProperty, null);
WriteValueToTextBox(string.Empty);
}
else
this.SetCurrentValue(SelectedDateTimeProperty, null);
if (SelectedDateTime == null)
{
// if already null, overwrite wrong data in textbox
WriteValueToTextBox();
}
}
Expand Down
30 changes: 15 additions & 15 deletions src/MahApps.Metro/Controls/TimePicker/TimePickerBase.cs
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,6 @@ public abstract class TimePickerBase : Control
private UIElement _secondHand;
private Selector _secondInput;
protected DatePickerTextBox _textBox;
protected bool _deactivateAdjustTimeOnDateChange;
protected DateTime? _originalSelectedDateTime;

public static readonly DependencyProperty SourceHoursProperty = DependencyProperty.Register(
Expand Down Expand Up @@ -528,9 +527,13 @@ protected virtual string GetValueForTextBox()
return valueForTextBox;
}

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

protected abstract void ClockSelectedTimeChanged(object sender, SelectionChangedEventArgs e);
this.SetCurrentValue(SelectedDateTimeProperty, date.Date + time);
}

protected void RaiseSelectedDateTimeChangedEvent(DateTime? oldValue, DateTime? newValue)
{
Expand Down Expand Up @@ -670,21 +673,16 @@ protected virtual void OnPopUpClosed()
// nothing here
}

protected void WriteValueToTextBox(string value)
protected virtual void WriteValueToTextBox()
{
if (_textBox != null)
{
_deactivateTextChangedEvent = true;
_textBox.Text = value;
_textBox.Text = GetValueForTextBox();
_deactivateTextChangedEvent = false;
}
}

protected virtual void WriteValueToTextBox()
{
WriteValueToTextBox(GetValueForTextBox());
}

private static IList<int> CreateValueList(int interval)
{
return Enumerable.Repeat(interval, 60 / interval)
Expand Down Expand Up @@ -748,7 +746,7 @@ private void TimePickerSelectionChanged(object sender, SelectionChangedEventArgs
return;
}

this.ClockSelectedTimeChanged(sender, e);
this.ClockSelectedTimeChanged();
}

private static void OnCultureChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
Expand Down Expand Up @@ -882,7 +880,7 @@ private static void SetDefaultTimeOfDayValue(Selector selector)
return new TimeSpan(hours, minutes, seconds);
}

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

Expand Down Expand Up @@ -956,7 +954,7 @@ private void TogglePopUp()
}
else
{
// SetSelectedDate();
SetSelectedDateTime();
this.SetCurrentValue(IsDropDownOpenProperty, true);
}
}
Expand Down Expand Up @@ -1072,21 +1070,23 @@ private bool ProcessKey(KeyEventArgs e)

case Key.Enter:
{
// SetSelectedDate();
SetSelectedDateTime();
return true;
}
}

return false;
}

protected abstract void SetSelectedDateTime();

private void TextBox_LostFocus(object sender, RoutedEventArgs e)
{
if (_textInputChanged)
{
_textInputChanged = false;

OnTextBoxLostFocus(sender, e);
SetSelectedDateTime();
}
}

Expand Down

0 comments on commit 3e47365

Please sign in to comment.