There are not very many options for a virtualizing wrap panel for use in WPF. For one reason or another MS decided to not ship one in the standard library.
If anyon
Explanation of the problem
You asked for an explanation of what is going wrong as well as instructions how to fix it. So far nobody has explained the problem. I will do so.
In ListBox with a VirtualizingWrapPanel there are five separate data structures that track items, each in different ways:
When an item is removed from ItemsSource, this removal must be propagated through all data structures. Here is how it works:
Because of this, the InternalChildren collection is out of sync with the other four collections, leading to the errors that were experienced.
Solution to the problem
To fix the problem, add the following code anywhere within VirtualizingWrapPanel's OnItemsChanged method:
switch(args.Action)
{
case NotifyCollectionChangedAction.Remove:
case NotifyCollectionChangedAction.Replace:
RemoveInternalChildRange(args.Position.Index, args.ItemUICount);
break;
case NotifyCollectionChangedAction.Move:
RemoveInternalChildRange(args.OldPosition.Index, args.ItemUICount);
break;
}
This keeps the InternalChildren collection in sync with the other data structures.
Why AddInternalChild/InsertInternalChild is not called here
You may wonder why there are no calls to InsertInternalChild or AddInternalChild in the above code, and especially why handling Replace and Move don't require us to add a new item during OnItemsChanged.
The key to understanding this is in the way ItemContainerGenerator works.
When ItemContainerGenerator receives a remove event it handles everything immediately:
On the other hand, ItemContainerGenerator learns that an item is added everything is typically deferred:
Thus, all removals from the InternalChildren collection (including ones that are part of a Move or Replace) must be done inside OnItemsChanged, but additions can (and should) be deferred until the next MeasureOverride.