Skip to content

Commit 98f9969

Browse files
kvptKevin PetitKeboo
authored
Prevent TimePicker to overwrite date (#1691)
* Prevent TimePicker to overwrite date. * Fix tests. * Mild cleanup of unit tests. Co-authored-by: Kevin Petit <[email protected]> Co-authored-by: Kevin Bost <[email protected]>
1 parent a8aadef commit 98f9969

File tree

2 files changed

+19
-10
lines changed

2 files changed

+19
-10
lines changed

MaterialDesignThemes.Wpf.Tests/TimePickerUnitTests.cs

Lines changed: 16 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
1-
using System;
1+
using System;
22
using System.Collections.Generic;
3+
using System.ComponentModel;
34
using System.Globalization;
45
using System.Windows;
56
using System.Windows.Controls;
@@ -19,6 +20,17 @@ public TimePickerUnitTests()
1920
_timePicker.ApplyDefaultStyle();
2021
}
2122

23+
[StaFact]
24+
[Description("Issue 1691")]
25+
public void DontOverwriteDate()
26+
{
27+
var expectedDate = new DateTime(2000, 1, 1, 20, 0, 0);
28+
29+
_timePicker.SelectedTime = expectedDate;
30+
31+
Assert.Equal(_timePicker.SelectedTime, expectedDate);
32+
}
33+
2234
[StaTheory]
2335
[MemberData(nameof(GetDisplaysExpectedTextData))]
2436
public void DisplaysExpectedText(CultureInfo culture, DatePickerFormat format, bool is24Hour, bool withSeconds,
@@ -30,9 +42,7 @@ public void DisplaysExpectedText(CultureInfo culture, DatePickerFormat format, b
3042
_timePicker.WithSeconds = withSeconds;
3143
_timePicker.SelectedTime = selectedTime;
3244

33-
34-
string currentTestString = $"{culture.ThreeLetterISOLanguageName} {(is24Hour ? "24 Hour" : "12 Hour")} {format} format {(withSeconds ? "with seconds" : "")}";
35-
Assert.True(expectedText == _timePicker.Text, $"Expected '{expectedText}' but was '{_timePicker.Text}' - {currentTestString}");
45+
Assert.Equal(expectedText, _timePicker.Text);
3646
}
3747

3848
[StaTheory]
@@ -49,8 +59,7 @@ public void CanParseLocalizedTimeString(CultureInfo culture, DatePickerFormat fo
4959
textBox.Text = timeString;
5060
textBox.RaiseEvent(new RoutedEventArgs(UIElement.LostFocusEvent));
5161

52-
string currentTestString = $"{culture.ThreeLetterISOLanguageName} {(is24Hour ? "24 Hour" : "12 Hour")} {format} format {(withSeconds ? "with seconds" : "")}";
53-
Assert.True(expectedTime == _timePicker.SelectedTime, $"Expected '{expectedTime}' but was '{_timePicker.SelectedTime}' - {currentTestString}");
62+
Assert.Equal(expectedTime, _timePicker.SelectedTime);
5463
}
5564

5665
public static IEnumerable<object[]> GetParseLocalizedTimeStringData()
@@ -65,7 +74,7 @@ public static IEnumerable<object[]> GetParseLocalizedTimeStringData()
6574
var timeString = (string) data[5];
6675

6776
//Convert the date to Today
68-
date = DateTime.Today.AddHours(date.Hour).AddMinutes(date.Minute).AddSeconds(withSeconds ? date.Second : 0);
77+
date = DateTime.MinValue.AddHours(date.Hour).AddMinutes(date.Minute).AddSeconds(withSeconds ? date.Second : 0);
6978

7079
if (!is24Hour && date.Hour > 12 &&
7180
(string.IsNullOrEmpty(culture.DateTimeFormat.AMDesignator) ||

MaterialDesignThemes.Wpf/TimePicker.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -299,7 +299,7 @@ private void TextBoxOnLostFocus(object sender, RoutedEventArgs routedEventArgs)
299299
}
300300

301301
if (IsTimeValid(_textBox.Text, out DateTime time))
302-
SetCurrentValue(SelectedTimeProperty, time);
302+
SetCurrentValue(SelectedTimeProperty, SelectedTime?.Date.Add(time.TimeOfDay) ?? time);
303303

304304
else // Invalid time, jump back to previous good time
305305
SetInvalidTime();
@@ -378,7 +378,7 @@ private void SetSelectedTime(bool beCautious = false)
378378
ParseTime(_textBox.Text, t =>
379379
{
380380
if (!beCautious || DateTimeToString(t) == _textBox.Text)
381-
SetCurrentValue(SelectedTimeProperty, t);
381+
SetCurrentValue(SelectedTimeProperty, SelectedTime?.Date.Add(t.TimeOfDay) ?? t);
382382
});
383383
}
384384
else
@@ -399,7 +399,7 @@ private bool IsTimeValid(string s, out DateTime time)
399399

400400
return DateTime.TryParse(s,
401401
culture,
402-
DateTimeStyles.AssumeLocal | DateTimeStyles.AllowWhiteSpaces,
402+
DateTimeStyles.AssumeLocal | DateTimeStyles.AllowWhiteSpaces | DateTimeStyles.NoCurrentDateDefault,
403403
out time);
404404
}
405405

0 commit comments

Comments
 (0)