From 990852383eb6b0993690039558b30bd21b62a400 Mon Sep 17 00:00:00 2001 From: msojocs Date: Tue, 19 Aug 2025 18:45:36 +0800 Subject: [PATCH] Fix: RelativePanel calculation error with extensible and aligned child (#19474) * Feat: Add unit test for RelativePanel. The ScrollViewer's scrollbar does not display correctly. * Fix: RelativePanel calculation error The ScrollViewer's scrollbar does not display correctly. --- src/Avalonia.Controls/RelativePanel.cs | 24 ++-- .../Xaml/RelativePanelTests.cs | 116 ++++++++++++++++++ 2 files changed, 132 insertions(+), 8 deletions(-) create mode 100644 tests/Avalonia.Markup.Xaml.UnitTests/Xaml/RelativePanelTests.cs diff --git a/src/Avalonia.Controls/RelativePanel.cs b/src/Avalonia.Controls/RelativePanel.cs index 274fa45fec..9976f31fc2 100644 --- a/src/Avalonia.Controls/RelativePanel.cs +++ b/src/Avalonia.Controls/RelativePanel.cs @@ -333,6 +333,22 @@ namespace Avalonia.Controls : node.AlignBottomWithNode.Bottom * 0.5; } + if (node.BelowNode != null) + { + if (node.Top.IsNaN()) + { + node.Top = AvailableSize.Height - node.BelowNode.Bottom; + } + } + + if (node.RightOfNode != null) + { + if (node.Left.IsNaN()) + { + node.Left = AvailableSize.Width - node.RightOfNode.Right; + } + } + var availableHeight = AvailableSize.Height - node.Top - node.Bottom; if (availableHeight.IsNaN()) { @@ -383,10 +399,6 @@ namespace Avalonia.Controls node.Right = node.RightOfNode.Right - childSize.Width; } - if (node.Left.IsNaN()) - { - node.Left = AvailableSize.Width - node.RightOfNode.Right; - } } if (node.BelowNode != null) @@ -396,10 +408,6 @@ namespace Avalonia.Controls node.Bottom = node.BelowNode.Bottom - childSize.Height; } - if (node.Top.IsNaN()) - { - node.Top = AvailableSize.Height - node.BelowNode.Bottom; - } } if (node.AlignHorizontalCenterWith != null) diff --git a/tests/Avalonia.Markup.Xaml.UnitTests/Xaml/RelativePanelTests.cs b/tests/Avalonia.Markup.Xaml.UnitTests/Xaml/RelativePanelTests.cs new file mode 100644 index 0000000000..84fec63139 --- /dev/null +++ b/tests/Avalonia.Markup.Xaml.UnitTests/Xaml/RelativePanelTests.cs @@ -0,0 +1,116 @@ +using System; +using System.Collections; +using System.Collections.Generic; +using System.Globalization; +using System.Linq; +using System.Reactive.Subjects; +using System.Threading; +using Avalonia.Controls; +using Avalonia.Data.Converters; +using Avalonia.UnitTests; +using Xunit; + +namespace Avalonia.Markup.Xaml.UnitTests.Xaml +{ + public class RelativePanelTests : XamlTestBase + { + [Fact] + public void ScrollViewer_Viewport_Small_Than_Bounds() + { + using (UnitTestApplication.Start(TestServices.StyledWindow)) + { + var xaml = @" + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +"; + var window = (Window)AvaloniaRuntimeXamlLoader.Load(xaml); + { + var panel = window.FindControl("TestRelativePanel"); + + panel.DataContext = new + { + DataExample = Enumerable.Range(1001, 100).Select(e => new { Id = $"{e}" }) + }; + } + window.ApplyTemplate(); + window.Show(); + + var sv = window.FindControl("TestArea"); + Assert.True(sv.Viewport.Width < sv.Bounds.Width); + Assert.True(sv.Viewport.Height < sv.Bounds.Height); + } + } + + } +}