TimePicker always returns today
See original GitHub issueHi,
I have found an issue with the way I would like to use the TimePicker control, which shows up if you are trying to link a DatePicker (MyVariable
bound to SelectedDate
) and a TimePicker (MyVariable
bound to SelectedTime
) to manipulate the different date/time portions of the same DateTime property.
If you change the date picker to an alternative date, then select the TimePicker button to show the popup menu it eventually hits the SetSelectedTime() Function and because the text box already contains a string value representing the currently set time, it parses just the time string and thus will always return today as it has no date element to parse.
Call stack: TogglePopup() => SetSelectedTime()
Existing Function:
private void SetSelectedTime(bool beCautious = false)
{
if (!string.IsNullOrEmpty(_textBox?.Text))
{
ParseTime(_textBox.Text, t =>
{
if (!beCautious || DateTimeToString(t) == _textBox.Text)
SetCurrentValue(SelectedTimeProperty, t);
});
}
else
{
SetCurrentValue(SelectedTimeProperty, null);
}
}
Maybe a check to see if the SelectedTime property is already set would be useful?
Suggested fix:
private void SetSelectedTime(bool beCautious = false)
{
if(SelectedTime == null)
{
if (!string.IsNullOrEmpty(_textBox?.Text))
{
ParseTime(_textBox.Text, t =>
{
if (!beCautious || DateTimeToString(t) == _textBox.Text)
SetCurrentValue(SelectedTimeProperty, t);
});
}
else
{
SetCurrentValue(SelectedTimeProperty, null);
}
}
}
Issue Analytics
- State:
- Created 6 years ago
- Comments:7 (6 by maintainers)
This seems to be already fixed with #1691
@MichelMichels Initially, am not sure switching to a TimeSpan is the right option (would love to hear other people feedback too). As I see it, the benefits of a TimeSpan are that it doesn’t have those messy date related properties. The downside would be that a TimeSpan represents a duration of time, not a single point in time. Most people implicitly assume a fixed starting offset and add the timespan to it (00:00:00 + time span = point in time). The other drawback is we then need to handle un-usual points in time, such as TimeSpan greater than 24 hours, or timespan that land on daylight savings time.
IMO, I think DateTime is a bit more clear that we are picking an individual point in time rather than a duration, so I like it better. Again, open to hearing other opinions.