diff --git a/src/Avalonia.Controls/DateTimePickers/TimePicker.cs b/src/Avalonia.Controls/DateTimePickers/TimePicker.cs index 5ad110eb693..c9e62e2cb27 100644 --- a/src/Avalonia.Controls/DateTimePickers/TimePicker.cs +++ b/src/Avalonia.Controls/DateTimePickers/TimePicker.cs @@ -259,12 +259,13 @@ private void SetGrid() return; var use24HourClock = ClockIdentifier == "24HourClock"; + var canUseSeconds = _secondText is not null && _fourthPickerHost is not null && _thirdSplitter is not null; var columnsD = new ColumnDefinitions(); columnsD.Add(new ColumnDefinition(GridLength.Star)); columnsD.Add(new ColumnDefinition(GridLength.Auto)); columnsD.Add(new ColumnDefinition(GridLength.Star)); - if (UseSeconds) + if (canUseSeconds && UseSeconds) { columnsD.Add(new ColumnDefinition(GridLength.Auto)); columnsD.Add(new ColumnDefinition(GridLength.Star)); @@ -274,30 +275,45 @@ private void SetGrid() columnsD.Add(new ColumnDefinition(GridLength.Auto)); columnsD.Add(new ColumnDefinition(GridLength.Star)); } - - _contentGrid.ColumnDefinitions = columnsD; - - _thirdPickerHost!.IsVisible = UseSeconds; - _secondSplitter!.IsVisible = UseSeconds; - _fourthPickerHost!.IsVisible = !use24HourClock; - _thirdSplitter!.IsVisible = !use24HourClock; + _contentGrid.ColumnDefinitions = columnsD; - var amPmColumn = (UseSeconds) ? 6 : 4; + if (canUseSeconds) + { + _thirdPickerHost!.IsVisible = UseSeconds; + _secondSplitter!.IsVisible = UseSeconds; + _fourthPickerHost!.IsVisible = !use24HourClock; + _thirdSplitter!.IsVisible = !use24HourClock; + } + else + { + _thirdPickerHost!.IsVisible = !use24HourClock; + _secondSplitter!.IsVisible = !use24HourClock; + } Grid.SetColumn(_firstPickerHost!, 0); Grid.SetColumn(_secondPickerHost!, 2); - Grid.SetColumn(_thirdPickerHost!, UseSeconds ? 4 : 0); - Grid.SetColumn(_fourthPickerHost, use24HourClock ? 0 : amPmColumn); - Grid.SetColumn(_firstSplitter!, 1); - Grid.SetColumn(_secondSplitter!, UseSeconds ? 3 : 0); - Grid.SetColumn(_thirdSplitter, use24HourClock ? 0 : amPmColumn-1); + if (canUseSeconds) + { + var amPmColumn = (UseSeconds) ? 6 : 4; + Grid.SetColumn(_thirdPickerHost!, UseSeconds ? 4 : 0); + Grid.SetColumn(_fourthPickerHost!, use24HourClock ? 0 : amPmColumn); + Grid.SetColumn(_firstSplitter!, 1); + Grid.SetColumn(_secondSplitter!, UseSeconds ? 3 : 0); + Grid.SetColumn(_thirdSplitter!, use24HourClock ? 0 : amPmColumn-1); + } + else + { + Grid.SetColumn(_thirdPickerHost, use24HourClock ? 0 : 4); + Grid.SetColumn(_firstSplitter!, 1); + Grid.SetColumn(_secondSplitter, use24HourClock ? 0 : 3); + } } private void SetSelectedTimeText() { - if (_hourText == null || _minuteText == null || _secondText == null || _periodText == null) + if (_hourText == null || _minuteText == null || _periodText == null) return; var time = SelectedTime; @@ -314,7 +330,11 @@ private void SetSelectedTimeText() _hourText.Text = newTime.ToString("%h"); _minuteText.Text = newTime.ToString("mm"); - _secondText.Text = newTime.ToString("ss"); + if (_secondText is not null) + { + _secondText.Text = newTime.ToString("ss"); + } + PseudoClasses.Set(":hasnotime", false); _periodText.Text = time.Value.Hours >= 12 ? TimeUtils.GetPMDesignator() : TimeUtils.GetAMDesignator(); @@ -324,7 +344,11 @@ private void SetSelectedTimeText() // By clearing local value, we reset text property to the value from the template. _hourText.ClearValue(TextBlock.TextProperty); _minuteText.ClearValue(TextBlock.TextProperty); - _secondText.ClearValue(TextBlock.TextProperty); + if (_secondText is not null) + { + _secondText.ClearValue(TextBlock.TextProperty); + } + PseudoClasses.Set(":hasnotime", true); _periodText.Text = DateTime.Now.Hour >= 12 ? TimeUtils.GetPMDesignator() : TimeUtils.GetAMDesignator(); diff --git a/src/Avalonia.Controls/DateTimePickers/TimePickerPresenter.cs b/src/Avalonia.Controls/DateTimePickers/TimePickerPresenter.cs index 9970efb9005..7772ef0bf42 100644 --- a/src/Avalonia.Controls/DateTimePickers/TimePickerPresenter.cs +++ b/src/Avalonia.Controls/DateTimePickers/TimePickerPresenter.cs @@ -144,15 +144,15 @@ protected override void OnApplyTemplate(TemplateAppliedEventArgs e) _pickerContainer = e.NameScope.Get("PART_PickerContainer"); _periodHost = e.NameScope.Get("PART_PeriodHost"); - _secondHost = e.NameScope.Get("PART_SecondHost"); + _secondHost = e.NameScope.Find("PART_SecondHost"); _hourSelector = e.NameScope.Get("PART_HourSelector"); _minuteSelector = e.NameScope.Get("PART_MinuteSelector"); - _secondSelector = e.NameScope.Get("PART_SecondSelector"); + _secondSelector = e.NameScope.Find("PART_SecondSelector"); _periodSelector = e.NameScope.Get("PART_PeriodSelector"); _spacer2 = e.NameScope.Get("PART_SecondSpacer"); - _spacer3 = e.NameScope.Get("PART_ThirdSpacer"); + _spacer3 = e.NameScope.Find("PART_ThirdSpacer"); _acceptButton = e.NameScope.Get