@@ -72,6 +72,8 @@ public class VirtualizingStackPanel : VirtualizingPanel, IScrollSnapPointsInfo
72
72
private Dictionary < object , Stack < Control > > ? _recyclePool ;
73
73
private Control ? _focusedElement ;
74
74
private int _focusedIndex = - 1 ;
75
+ private Control ? _realizingElement ;
76
+ private int _realizingIndex = - 1 ;
75
77
76
78
public VirtualizingStackPanel ( )
77
79
{
@@ -336,6 +338,8 @@ protected override void OnItemsChanged(IReadOnlyList<object?> items, NotifyColle
336
338
return _scrollToElement ;
337
339
if ( _focusedIndex == index )
338
340
return _focusedElement ;
341
+ if ( index == _realizingIndex )
342
+ return _realizingElement ;
339
343
if ( GetRealizedElement ( index ) is { } realized )
340
344
return realized ;
341
345
if ( Items [ index ] is Control c && c . GetValue ( RecycleKeyProperty ) == s_itemIsItsOwnContainer )
@@ -349,6 +353,8 @@ protected internal override int IndexFromContainer(Control container)
349
353
return _scrollToIndex ;
350
354
if ( container == _focusedElement )
351
355
return _focusedIndex ;
356
+ if ( container == _realizingElement )
357
+ return _realizingIndex ;
352
358
return _realizedElements ? . GetIndex ( container ) ?? - 1 ;
353
359
}
354
360
@@ -532,7 +538,9 @@ private void RealizeElements(
532
538
// Start at the anchor element and move forwards, realizing elements.
533
539
do
534
540
{
541
+ _realizingIndex = index ;
535
542
var e = GetOrCreateElement ( items , index ) ;
543
+ _realizingElement = e ;
536
544
e . Measure ( availableSize ) ;
537
545
538
546
var sizeU = horizontal ? e . DesiredSize . Width : e . DesiredSize . Height ;
@@ -543,6 +551,8 @@ private void RealizeElements(
543
551
544
552
u += sizeU ;
545
553
++ index ;
554
+ _realizingIndex = - 1 ;
555
+ _realizingElement = null ;
546
556
} while ( u < viewport . viewportUEnd && index < items . Count ) ;
547
557
548
558
// Store the last index and end U position for the desired size calculation.
0 commit comments