Browse Source

fix for ListBox OutOfRangeException issue #1395

Andrey Kunchev 7 years ago
parent
commit
092a7c965a
1 changed files with 31 additions and 3 deletions
  1. 31 3
      src/Avalonia.Controls/Presenters/ItemVirtualizerSimple.cs

+ 31 - 3
src/Avalonia.Controls/Presenters/ItemVirtualizerSimple.cs

@@ -76,9 +76,23 @@ namespace Avalonia.Controls.Presenters
                         var firstIndex = ItemCount - panel.Children.Count;
                         RecycleContainersForMove(firstIndex - FirstIndex);
 
-                        panel.PixelOffset = VirtualizingPanel.ScrollDirection == Orientation.Vertical ?
-                            panel.Children[0].Bounds.Height :
-                            panel.Children[0].Bounds.Width;
+                        double pixelOffset;
+                        var child = panel.Children[0];
+
+                        if (child.IsArrangeValid)
+                        {
+                            pixelOffset = VirtualizingPanel.ScrollDirection == Orientation.Vertical ?
+                                                    child.Bounds.Height :
+                                                    child.Bounds.Width;
+                        }
+                        else
+                        {
+                            pixelOffset = VirtualizingPanel.ScrollDirection == Orientation.Vertical ?
+                                                    child.DesiredSize.Height :
+                                                    child.DesiredSize.Width;
+                        }
+
+                        panel.PixelOffset = pixelOffset;
                     }
                 }
             }
@@ -402,6 +416,20 @@ namespace Avalonia.Controls.Presenters
             var panel = VirtualizingPanel;
             var generator = Owner.ItemContainerGenerator;
             var selector = Owner.MemberSelector;
+
+            //validate delta it should never overflow last index or generate index < 0 
+            if (delta > 0)
+            {
+                if ((FirstIndex + delta + panel.Children.Count) > ItemCount)
+                {
+                    delta = ItemCount - FirstIndex - panel.Children.Count;
+                }
+            }
+            else if ((FirstIndex + delta) < 0)
+            {
+                delta = -FirstIndex;
+            }
+
             var sign = delta < 0 ? -1 : 1;
             var count = Math.Min(Math.Abs(delta), panel.Children.Count);
             var move = count < panel.Children.Count;