From daa8bd22dfde8a7da0ea735c431e30b419f03538 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jo=C3=A3o=20Cruz?= Date: Sun, 24 May 2026 12:17:31 +0100 Subject: [PATCH] Add VerticalContentAlignment property to DatePicker and TimePicker This change registers VerticalContentAlignmentProperty as a StyledProperty using ContentControl's property as the owner for both DatePicker and TimePicker controls to maintain framework consistency. Basic xUnit v3 unit tests are introduced to validate property round-trips (Top, Center, Bottom, Stretch) and to ensure the default value correctly resolves to Stretch. This establishes the necessary C# infrastructure before modifying the control templates. Part of #21211: implment fluent theme layout --- .../DateTimePickers/DatePicker.cs | 15 +++++++++++++++ .../DateTimePickers/TimePicker.cs | 14 ++++++++++++++ .../DatePickerTests.cs | 18 ++++++++++++++++++ .../TimePickerTests.cs | 18 ++++++++++++++++++ 4 files changed, 65 insertions(+) diff --git a/src/Avalonia.Controls/DateTimePickers/DatePicker.cs b/src/Avalonia.Controls/DateTimePickers/DatePicker.cs index 2f406252f7..c4206c7315 100644 --- a/src/Avalonia.Controls/DateTimePickers/DatePicker.cs +++ b/src/Avalonia.Controls/DateTimePickers/DatePicker.cs @@ -75,6 +75,21 @@ namespace Avalonia.Controls public static readonly StyledProperty YearVisibleProperty = AvaloniaProperty.Register(nameof(YearVisible), true); + /// + /// Defines the property. + /// + public static readonly StyledProperty VerticalContentAlignmentProperty = + ContentControl.VerticalContentAlignmentProperty.AddOwner(); + + /// + /// Gets or sets the vertical alignment of the content within the control. + /// + public VerticalAlignment VerticalContentAlignment + { + get => GetValue(VerticalContentAlignmentProperty); + set => SetValue(VerticalContentAlignmentProperty, value); + } + /// /// Defines the Property /// diff --git a/src/Avalonia.Controls/DateTimePickers/TimePicker.cs b/src/Avalonia.Controls/DateTimePickers/TimePicker.cs index 95d9e82782..7c6f96247f 100644 --- a/src/Avalonia.Controls/DateTimePickers/TimePicker.cs +++ b/src/Avalonia.Controls/DateTimePickers/TimePicker.cs @@ -50,6 +50,20 @@ namespace Avalonia.Controls public static readonly StyledProperty ClockIdentifierProperty = AvaloniaProperty.Register(nameof(ClockIdentifier), "12HourClock", coerce: CoerceClockIdentifier); + /// + /// Defines the property. + /// + public static readonly StyledProperty VerticalContentAlignmentProperty = + ContentControl.VerticalContentAlignmentProperty.AddOwner(); + + /// + /// Gets or sets the vertical alignment of the content within the control. + /// + public VerticalAlignment VerticalContentAlignment + { + get => GetValue(VerticalContentAlignmentProperty); + set => SetValue(VerticalContentAlignmentProperty, value); + } /// /// Defines the property /// diff --git a/tests/Avalonia.Controls.UnitTests/DatePickerTests.cs b/tests/Avalonia.Controls.UnitTests/DatePickerTests.cs index 4688a88fc5..523340a271 100644 --- a/tests/Avalonia.Controls.UnitTests/DatePickerTests.cs +++ b/tests/Avalonia.Controls.UnitTests/DatePickerTests.cs @@ -371,6 +371,24 @@ namespace Avalonia.Controls.UnitTests }); } + [Theory] + [InlineData(Avalonia.Layout.VerticalAlignment.Top)] + [InlineData(Avalonia.Layout.VerticalAlignment.Center)] + [InlineData(Avalonia.Layout.VerticalAlignment.Bottom)] + [InlineData(Avalonia.Layout.VerticalAlignment.Stretch)] + public void VerticalContentAlignment_RoundTrips(Avalonia.Layout.VerticalAlignment value) + { + var datePicker = new DatePicker { VerticalContentAlignment = value }; + Assert.Equal(value, datePicker.VerticalContentAlignment); + } + + [Fact] + public void VerticalContentAlignment_Default_Is_Stretch() + { + var datePicker = new DatePicker(); + Assert.Equal(Avalonia.Layout.VerticalAlignment.Stretch, datePicker.VerticalContentAlignment); + } + private static IControlTemplate CreatePickerTemplate() { return new FuncControlTemplate((_, scope) => diff --git a/tests/Avalonia.Controls.UnitTests/TimePickerTests.cs b/tests/Avalonia.Controls.UnitTests/TimePickerTests.cs index 11c52392f1..2c16c19f60 100644 --- a/tests/Avalonia.Controls.UnitTests/TimePickerTests.cs +++ b/tests/Avalonia.Controls.UnitTests/TimePickerTests.cs @@ -466,5 +466,23 @@ namespace Avalonia.Controls.UnitTests return contentPanel; }); } + + [Theory] + [InlineData(Avalonia.Layout.VerticalAlignment.Top)] + [InlineData(Avalonia.Layout.VerticalAlignment.Center)] + [InlineData(Avalonia.Layout.VerticalAlignment.Bottom)] + [InlineData(Avalonia.Layout.VerticalAlignment.Stretch)] + public void VerticalContentAlignment_RoundTrips(Avalonia.Layout.VerticalAlignment value) + { + var timePicker = new TimePicker { VerticalContentAlignment = value }; + Assert.Equal(value, timePicker.VerticalContentAlignment); + } + + [Fact] + public void VerticalContentAlignment_Default_Is_Stretch() + { + var timePicker = new TimePicker(); + Assert.Equal(Avalonia.Layout.VerticalAlignment.Stretch, timePicker.VerticalContentAlignment); + } } }