diff --git a/src/Avalonia.Controls/VirtualizingStackPanel.cs b/src/Avalonia.Controls/VirtualizingStackPanel.cs index 9893c81acc..cae0d0a15d 100644 --- a/src/Avalonia.Controls/VirtualizingStackPanel.cs +++ b/src/Avalonia.Controls/VirtualizingStackPanel.cs @@ -165,7 +165,7 @@ namespace Avalonia.Controls // We handle horizontal and vertical layouts here so X and Y are abstracted to: // - Horizontal layouts: U = horizontal, V = vertical // - Vertical layouts: U = vertical, V = horizontal - var viewport = CalculateMeasureViewport(items); + var viewport = CalculateMeasureViewport(orientation, items); // If the viewport is disjunct then we can recycle everything. if (viewport.viewportIsDisjunct) @@ -449,15 +449,15 @@ namespace Avalonia.Controls return _realizedElements?.Elements ?? Array.Empty(); } - private MeasureViewport CalculateMeasureViewport(IReadOnlyList items) + private MeasureViewport CalculateMeasureViewport(Orientation orientation, IReadOnlyList items) { Debug.Assert(_realizedElements is not null); var viewport = _viewport; // Get the viewport in the orientation direction. - var viewportStart = Orientation == Orientation.Horizontal ? viewport.X : viewport.Y; - var viewportEnd = Orientation == Orientation.Horizontal ? viewport.Right : viewport.Bottom; + var viewportStart = orientation == Orientation.Horizontal ? viewport.X : viewport.Y; + var viewportEnd = orientation == Orientation.Horizontal ? viewport.Right : viewport.Bottom; // Get or estimate the anchor element from which to start realization. If we are // scrolling to an element, use that as the anchor element. Otherwise, estimate the @@ -468,7 +468,7 @@ namespace Avalonia.Controls if (_scrollToIndex >= 0 && _scrollToElement is not null) { anchorIndex = _scrollToIndex; - anchorU = _scrollToElement.Bounds.Top; + anchorU = orientation == Orientation.Horizontal ? _scrollToElement.Bounds.Left : _scrollToElement.Bounds.Top; } else {