WPF中如何正确实现VirtualizingWrapPanel

How to correctly implement VirtualizingWrapPanel in WPF

我对 WPF 有点陌生,我的应用程序性能存在一些问题。解决方案应该是 VirtualizingWrapPanel。我做了一些研究,并没有很多教程或文档来解释如何正确使用它。我下载了一个示例项目并构建了一个 DLL (https://github.com/sbaeumlisberger/VirtualizingWrapPanel)。然后我在我的程序中引用了这个并尝试了这个代码:

<vwp:VirtualizingItemsControl VirtualizingPanel.CacheLengthUnit="Item" VirtualizingPanel.ScrollUnit="Pixel" VirtualizingPanel.VirtualizationMode="Recycling">
    <ItemsControl.ItemsPanel>
        <ItemsPanelTemplate>
            <vwp:VirtualizingWrapPanel Orientation="Vertical" SpacingMode="None" StretchItems="False"/>
        </ItemsPanelTemplate>
    </ItemsControl.ItemsPanel>
            
    <local:my_custom_control/>
    <local:my_custom_control/>
    <local:my_custom_control/>
    <local:my_custom_control/>
    <local:my_custom_control/>
    <local:my_custom_control/>
    ...

</vwp:VirtualizingItemsControl>

但是,此代码不起作用,并且具有与普通包裹面板完全相同的性能。有谁知道问题出在哪里?如果这听起来很愚蠢或类似的东西,我很抱歉,我对这些东西有点陌生。

经过多次尝试,我明白了。这是我的代码:

Xaml:

<vwp:VirtualizingItemsControl Name="vic" 
                               VirtualizingPanel.CacheLengthUnit="Item" 
                               VirtualizingPanel.ScrollUnit="Pixel" 
                               VirtualizingPanel.VirtualizationMode="Recycling"
                               VirtualizingPanel.IsVirtualizingWhenGrouping="True"
                               >
     <ItemsControl.ItemsPanel>
         <ItemsPanelTemplate>
             <vwp:VirtualizingWrapPanel Orientation="Vertical" SpacingMode="None" StretchItems="False" />
         </ItemsPanelTemplate>
     </ItemsControl.ItemsPanel>
 </vwp:VirtualizingItemsControl>

C#:

List<Button> buttons = new List<Button>();

list.add(new Button() { Content = "Name", Width = 90, Height = 40 });

vic.ItemsSource = buttons;
ICollectionView cv = CollectionViewSource.GetDefaultView(vic.ItemsSource);