ListBox Grouping and Virtualization
Ensure the panels discover each other in time for layout.
We need to ensure the panels learn about each other before the very first layout pass, or virtualization will go awry. Here’s a summary of how to do it:

In your INestedPanel, override OnInitialized() and walk the visual tree, using the VisualTreeHelper class, to find the IOuterPanel instance and store it off. We do this here because when it’s been initialized, the IOuterPanel should already be constructed, and because OnInitialized() occurs before the first layout pass, where we will need to get IOuterPanel.EstimatedViewport to calculate which items need to be generated for display on screen.
Next, have the nested panel call IOuterPanel.AddNestedPanel(). The grouping panel should then add the nested panel to a collection of INestedPanel references that it maintains. Remember, when you have grouping, you could have multiple GroupingItem objects that each wrap a separate instance of your ListBox.ItemsPanel. By maintaining a collection of the nested panels, the grouping panel can notify each one every time the scroll offsets change.
This entry was posted on February 22, 2008 at 9:04 pm and is filed under WPF. You can subscribe via RSS 2.0 feed to this post's comments.
Tags: grouping, lazy-loading, ListBox, panel, scrolling, virtualizing
You can comment below, or link to this permanent URL from your own site.
July 9, 2009 at 11:10 am
Great blog post! Can you please provide more details how you reworked your solution to support sorting? I am having trouble with virtualization when grouping is enabled and can’t seem to solve this issue.