2

有人可以告诉我如何在 WPF 中正确使用 VirtualizingStackPanel 吗?我已经在我的 ItemsPanelTemplate 中为我的 ItemsControl 设置了一个 VirtualizingStackPanel,并将其设置为我的 ItemsHost,但是当我在我的项目控件上注册 CleanUpVirtualizedItem 附加事件的侦听器时,没有任何反应。我没有看到该事件被调用。我也没有看到我的数据模板中的自定义控件上的任何 Unloaded 事件被调用,这向我表明没有实际的虚拟化正在进行。任何帮助将非常感激。

4

1 回答 1

5

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.

于 2011-01-25T00:42:17.300 回答