From a1d809b49dbceb8aa07b3205387a64d436db212f Mon Sep 17 00:00:00 2001 From: Steven Kirk Date: Wed, 30 Nov 2022 00:17:29 +0100 Subject: [PATCH] Fix VirtualizingStackPanel navigation logic. --- src/Avalonia.Controls/VirtualizingStackPanel.cs | 9 ++++++--- 1 file changed, 6 insertions(+), 3 deletions(-) diff --git a/src/Avalonia.Controls/VirtualizingStackPanel.cs b/src/Avalonia.Controls/VirtualizingStackPanel.cs index 2e80edd6bb..1016a0af59 100644 --- a/src/Avalonia.Controls/VirtualizingStackPanel.cs +++ b/src/Avalonia.Controls/VirtualizingStackPanel.cs @@ -173,11 +173,11 @@ namespace Avalonia.Controls { var count = (ItemsControl?.Items as IList)?.Count ?? 0; - if (count == 0) + if (count == 0 || from is not Control fromControl) return null; var horiz = Orientation == Orientation.Horizontal; - var fromIndex = from != null ? Children.IndexOf(from) : -1; + var fromIndex = from != null ? IndexFromContainer(fromControl) : -1; var toIndex = fromIndex; switch (direction) @@ -708,7 +708,10 @@ namespace Avalonia.Controls /// /// The element. /// The model index or -1 if the element is not present in the collection. - public int GetIndex(Control element) => _elements?.IndexOf(element) is int index && index >= 0 ? index : -1; + public int GetIndex(Control element) + { + return _elements?.IndexOf(element) is int index && index >= 0 ? index + FirstModelIndex : -1; + } /// /// Updates the elements in response to items being inserted into the source collection.