سؤال

Can somebody please tell me how to properly use the VirtualizingStackPanel in WPF ? I've set a VirtualizingStackPanel in my ItemsPanelTemplate for my ItemsControl, and set it as my ItemsHost, but when I register a listener for the CleanUpVirtualizedItem attached event on my items control, nothing happens. I don't see the event getting called. Nor do I see any of the Unloaded events on the custom controls in my data template getting called, indicating to me that there's no actual virtualization going on. Any help would be much appreciated.

هل كانت مفيدة؟

المحلول

Try using a ListBox or ListView instead:

<ListBox ItemsSource="{Binding Items}"
    VirtualizingStackPanel.VirtualizationMode="Recycling" 
    VirtualizingStackPanel.CleanUpVirtualizedItem="ItemsControl_CleanUpVirtualizedItem">
</ListBox>

The ListBox will automatically handle virtualization of the items - there is a bit more manual work to do if you create the VirtualizingStackPanel directly. This is because The default ControlTemplate for ItemsControl does not have a ScrollViewer, which is the key to virtualization - more info here.

مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top