将 ListView 设置为填充所有可用的垂直空间

4
我想设计一个页面,顶部行高固定,而ListView填充所有可用的剩余空间。
ListView有一个很大的ItemsSource,为了正确使用UI虚拟化,我读到需要指定高度。如果我将其设置为绝对值,则可以正常工作。但是,将其设置为“Stretch”似乎会渲染整个ListView(即使超出屏幕)。
<Grid Background="{StaticResource ApplicationPageBackgroundThemeBrush}">
    <Grid.RowDefinitions>
        <RowDefinition Height="200"></RowDefinition>
        <RowDefinition Height="*"></RowDefinition>
    </Grid.RowDefinitions>
    <ScrollViewer VerticalScrollMode="Enabled" Grid.Row="1">
        <ListView VerticalAlignment="Stretch"/>
    </ScrollViewer>
</Grid>

我该如何让ListView占据所有可用的空间,同时不超出屏幕范围?

1个回答

3
因为您将整个ListView放在了一个ScrollViewer中,所以该ListView会被完全渲染出来。独立的ScrollViewer不知道虚拟化,它告诉其内容可以在无限空间上渲染。
因此,移除ScrollViewer,让ListView成为Grid的直接子级。ListView有其自己的内部ScrollViewer,可以处理虚拟化,当ListView有足够多的项填满屏幕时,您应该会看到它的效果。
<Grid Background="{StaticResource ApplicationPageBackgroundThemeBrush}">
    <Grid.RowDefinitions>
        <RowDefinition Height="200"></RowDefinition>
        <RowDefinition Height="*"></RowDefinition>
    </Grid.RowDefinitions>

    <ListView VerticalAlignment="Stretch" Grid.Row="1" />
</Grid>

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接