多个 ListView UI 虚拟化

Multiple ListView UI Virtualization

我有类似下面的代码:

<ScrollViewer>
    <Grid>
        <Grid.RowDefinitions>
            <RowDefinition Height="Auto" />
            <RowDefinition Height="Auto" />
        </Grid.RowDefinitions>
        <ListView ItemsSource="{Binding FewItems}" />
        <ListView ItemsSource="{Binding ManyItems}" Grid.Row="1" />
    </Grid>
</ScrollViewer>

我的问题是第二个列表,它非常大。通常,UI 虚拟化会发生,但由于它是在滚动查看器和网格中定义的,因此列表会根据需要使用 space。当我加载此页面时,这会导致明显的滞后。

为列表设置最大高度没有任何好处,因为它会导致列表在外部滚动查看器中自行滚动。

我的问题是:

  1. 有没有办法强制列表使用虚拟化?
  2. 有没有办法在保持滚动行为的同时做到这一点,以便两个列表都可以作为单个控件滚动?

谢谢。

将其声明为 VirtualizingStackPanel

<ItemsControl.ItemsPanel>
    <ItemsPanelTemplate>
        <VirtualizingStackPanel />
    </ItemsPanelTemplate>
</ItemsControl.ItemsPanel>

。 . .

示例:

<ItemsControl.ItemsPanel>
    <ItemsPanelTemplate>
        <VirtualizingStackPanel />
    </ItemsPanelTemplate>
</ItemsControl.ItemsPanel>

<ItemsControl.Template>
    <ControlTemplate TargetType="ItemsControl">
        <Border>
            <ScrollViewer>
                <ItemsPresenter/>
            </ScrollViewer>
        </Border>
    </ControlTemplate>
</ItemsControl.Template>

<ItemsControl.ItemTemplate>
    <DataTemplate>
        <TextBlock Text="Hello Wworld" />
    </DataTemplate>
</ItemsControl.ItemTemplate>

正如您观察到的那样,虚拟化无法在滚动视图中工作。查看您的 XAML 代码,通过创建结合 FewItems 和 ManyItems 的中间集合并将该集合绑定到单个 ListView 应该很容易解决您的问题。如果您需要为 FewItems 中的项目设置不同于 ManyItems 中的项目的样式,您可能会遇到一些挑战,但这些可以通过样式选择器等来克服。