@@ -281,13 +281,13 @@ public void ItemsInserted(int index, int count, Action<Control, int, int> update
281
281
// elements after the insertion point.
282
282
var elementCount = _elements . Count ;
283
283
var start = Math . Max ( realizedIndex , 0 ) ;
284
- var newIndex = realizedIndex + count ;
285
284
286
285
for ( var i = start ; i < elementCount ; ++ i )
287
286
{
288
- if ( _elements [ i ] is Control element )
289
- updateElementIndex ( element , ( newIndex - count ) + first , newIndex + first ) ;
290
- ++ newIndex ;
287
+ if ( _elements [ i ] is not Control element )
288
+ continue ;
289
+ var oldIndex = i + first ;
290
+ updateElementIndex ( element , oldIndex , oldIndex + count ) ;
291
291
}
292
292
293
293
if ( realizedIndex < 0 )
@@ -341,7 +341,7 @@ public void ItemsRemoved(
341
341
for ( var i = 0 ; i < _elements . Count ; ++ i )
342
342
{
343
343
if ( _elements [ i ] is Control element )
344
- updateElementIndex ( element , newIndex - count , newIndex ) ;
344
+ updateElementIndex ( element , newIndex + count , newIndex ) ;
345
345
++ newIndex ;
346
346
}
347
347
}
@@ -384,6 +384,37 @@ public void ItemsRemoved(
384
384
}
385
385
}
386
386
387
+ /// <summary>
388
+ /// Updates the elements in response to items being replaced in the source collection.
389
+ /// </summary>
390
+ /// <param name="index">The index in the source collection of the remove.</param>
391
+ /// <param name="count">The number of items removed.</param>
392
+ /// <param name="recycleElement">A method used to recycle elements.</param>
393
+ public void ItemsReplaced ( int index , int count , Action < Control > recycleElement )
394
+ {
395
+ if ( index < 0 )
396
+ throw new ArgumentOutOfRangeException ( nameof ( index ) ) ;
397
+ if ( _elements is null || _elements . Count == 0 )
398
+ return ;
399
+
400
+ // Get the index within the realized _elements collection.
401
+ var startIndex = index - FirstIndex ;
402
+ var endIndex = Math . Min ( startIndex + count , Count ) ;
403
+
404
+ if ( startIndex >= 0 && endIndex > startIndex )
405
+ {
406
+ for ( var i = startIndex ; i < endIndex ; ++ i )
407
+ {
408
+ if ( _elements [ i ] is { } element )
409
+ {
410
+ recycleElement ( element ) ;
411
+ _elements [ i ] = null ;
412
+ _sizes ! [ i ] = double . NaN ;
413
+ }
414
+ }
415
+ }
416
+ }
417
+
387
418
/// <summary>
388
419
/// Recycles all elements in response to the source collection being reset.
389
420
/// </summary>
0 commit comments