Merge pull request #240 from irihitech/timepicker_fix

TimePicker Fixes
This commit is contained in:
Dong Bin
2024-05-18 13:53:02 +08:00
committed by GitHub
4 changed files with 64 additions and 30 deletions

View File

@@ -11,21 +11,36 @@
<StackPanel HorizontalAlignment="Left"> <StackPanel HorizontalAlignment="Left">
<ToggleSwitch Name="needConfirm" Content="Need Confirm" /> <ToggleSwitch Name="needConfirm" Content="Need Confirm" />
<TextBlock Text="{Binding #picker.SelectedTime}" /> <TextBlock Text="{Binding #picker.SelectedTime}" />
<TextBox
Name="displayFormat"
Width="300"
InnerLeftContent="Display Format"
Text="HH 时 mm 分 ss 秒" />
<TextBox
Name="panelFormat"
Width="300"
InnerLeftContent="Panel Format"
Text="tt HH mm ss" />
<u:TimePicker <u:TimePicker
Name="picker" Name="picker"
Width="200" Width="200"
HorizontalAlignment="Left" HorizontalAlignment="Left"
DisplayFormat="{Binding #displayFormat.Text}"
NeedConfirmation="{Binding #needConfirm.IsChecked}" NeedConfirmation="{Binding #needConfirm.IsChecked}"
PanelFormat="hh mm tt" /> PanelFormat="{Binding #panelFormat.Text}" />
<u:TimePicker <u:TimePicker
Width="300" Width="300"
Classes="ClearButton"
DisplayFormat="HH 时 mm 分 ss 秒"
PanelFormat="tt HH mm ss"
HorizontalAlignment="Left" HorizontalAlignment="Left"
NeedConfirmation="True" Classes="ClearButton"
DisplayFormat="{Binding #displayFormat.Text}"
InnerLeftContent="时刻" InnerLeftContent="时刻"
InnerRightContent="截止" /> InnerRightContent="截止"
<u:TimeRangePicker Width="300"></u:TimeRangePicker> NeedConfirmation="True"
PanelFormat="{Binding #panelFormat.Text}" />
<u:TimeRangePicker
Width="300"
DisplayFormat="{Binding #displayFormat.Text}"
PanelFormat="{Binding #panelFormat.Text}" />
</StackPanel> </StackPanel>
</UserControl> </UserControl>

View File

@@ -25,7 +25,7 @@
BorderBrush="{TemplateBinding BorderBrush}" BorderBrush="{TemplateBinding BorderBrush}"
BorderThickness="{TemplateBinding BorderThickness}" BorderThickness="{TemplateBinding BorderThickness}"
CornerRadius="{TemplateBinding CornerRadius}" /> CornerRadius="{TemplateBinding CornerRadius}" />
<Grid ColumnDefinitions="*, Auto, * Auto"> <Grid ColumnDefinitions="*, Auto, * Auto" Name="PART_Grid">
<TextBox <TextBox
Name="{x:Static u:TimeRangePicker.PART_StartTextBox}" Name="{x:Static u:TimeRangePicker.PART_StartTextBox}"
Grid.Column="0" Grid.Column="0"
@@ -75,6 +75,8 @@
<Popup <Popup
Name="{x:Static iri:PartNames.PART_Popup}" Name="{x:Static iri:PartNames.PART_Popup}"
IsLightDismissEnabled="True" IsLightDismissEnabled="True"
OverlayDismissEventPassThrough="True"
OverlayInputPassThroughElement="{Binding ElementName=PART_Grid}"
IsOpen="{TemplateBinding IsDropdownOpen, IsOpen="{TemplateBinding IsDropdownOpen,
Mode=TwoWay}" Mode=TwoWay}"
Placement="BottomEdgeAlignedLeft" Placement="BottomEdgeAlignedLeft"

View File

@@ -39,6 +39,16 @@ public class TimePicker : TimePickerBase, IClearControl
{ {
SelectedTimeProperty.Changed.AddClassHandler<TimePicker, TimeSpan?>((picker, args) => SelectedTimeProperty.Changed.AddClassHandler<TimePicker, TimeSpan?>((picker, args) =>
picker.OnSelectionChanged(args)); picker.OnSelectionChanged(args));
DisplayFormatProperty.Changed.AddClassHandler<TimePicker, string?>((picker, args) => picker.OnDisplayFormatChanged(args));
}
private void OnDisplayFormatChanged(AvaloniaPropertyChangedEventArgs<string?> args)
{
if (_textBox is null) return;
var time = SelectedTime;
var date = new DateTime( 1, 1, 1, time.Value.Hours, time.Value.Minutes, time.Value.Seconds);
var text = date.ToString(DisplayFormat);
_textBox.Text = text;
} }
public string? Watermark public string? Watermark

View File

@@ -130,17 +130,19 @@ public class TimePickerPresenter : TemplatedControl
private void OnPanelFormatChanged(AvaloniaPropertyChangedEventArgs<string> args) private void OnPanelFormatChanged(AvaloniaPropertyChangedEventArgs<string> args)
{ {
var format = args.NewValue.Value; var format = args.NewValue.Value;
UpdatePanelLayout(format); UpdatePanelLayout(format);
} }
private void UpdatePanelLayout(string panelFormat) private void UpdatePanelLayout(string? panelFormat)
{ {
if (panelFormat is null) return;
var parts = panelFormat.Split(new[] { ' ', '-', ':' }, StringSplitOptions.RemoveEmptyEntries); var parts = panelFormat.Split(new[] { ' ', '-', ':' }, StringSplitOptions.RemoveEmptyEntries);
var panels = new List<Control?>(); var panels = new List<Control?>();
foreach (var part in parts) foreach (var part in parts)
{ {
if (part.Length < 1) continue; if (part.Length < 1) continue;
try
{
if ((part.Contains('h') || part.Contains('H')) && !panels.Contains(_hourScrollPanel)) if ((part.Contains('h') || part.Contains('H')) && !panels.Contains(_hourScrollPanel))
{ {
panels.Add(_hourScrollPanel); panels.Add(_hourScrollPanel);
@@ -168,6 +170,11 @@ public class TimePickerPresenter : TemplatedControl
_ampmSelector?.SetValue(DateTimePickerPanel.ItemFormatProperty, part); _ampmSelector?.SetValue(DateTimePickerPanel.ItemFormatProperty, part);
} }
} }
catch
{
}
}
if (panels.Count < 1) return; if (panels.Count < 1) return;
IsVisibleProperty.SetValue(false, _hourScrollPanel, _minuteScrollPanel, _secondScrollPanel, _ampmScrollPanel, IsVisibleProperty.SetValue(false, _hourScrollPanel, _minuteScrollPanel, _secondScrollPanel, _ampmScrollPanel,