Everybody says the default ItemsPanel for a ListBox is a VirtualizingStackPanel. I created a ListBox-derived class (call it MyListBox) and it defaults to StackPanel instead.
I mean I have to force the virtualization, for example this way:
const string itemsPanelTemplateString = @"
<ItemsPanelTemplate
xmlns=""http://schemas.microsoft.com/winfx/2006/xaml/presentation""
xmlns:x=""http://schemas.microsoft.com/winfx/2006/xaml"" >
<VirtualizingStackPanel/>
</ItemsPanelTemplate>";
MyListBox {
this.ItemsPanel = (ItemsPanelTemplate)
System.Windows.Markup.XamlReader.Load(itemsPanelTemplateString);
}
I could reprint here my class, but that's not the point. I would like to know general answer.
The class does not change predefined ListBox style, but it uses own ListBoxItem-derived class.
I am pretty sure there are some conditions for using virtualization, as my colleague said he saw respective ListBox code in the past. Unfortunately right now we don't have access to the debug versions of MS dll's.
The default style for a ListBox does not assign the ItemsPanel template.
According to the internal code I can see in reflector
OnApplyTemplatewill assign aVirtualizingStackPanelto the internalItemsHostif aItemsPaneltemplate is not supplied.Perhaps including your class code might be a good idea after all.