我正在使用 WPF Datagrid 并且需要显示 10,000 行,因此需要虚拟化。从 StackOverflow 上的几篇文章中,我看到 WPF Datagrid 无法实现虚拟化 + 分组。这是因为用于渲染组的 Expander 模板无法虚拟化。

在我们的系统中,我们可能有 10,000 行,但每组中只有 3 或 4 行。此外,绝大多数行没有分组 - 它们的 GroupId 为空。在原型(prototype)中,我正在将这些渲染作为没有标题的组扩展器。我最喜欢的是那些不分组的,只是行,其余的渲染在扩展器中。这可能吗?

最佳答案

试试这个...

<GroupStyle.ContainerStyle>
    <Style TargetType="{x:Type GroupItem}">
        <Style.Resources>
            <ControlTemplate x:Key="MultiItemGroupTemplate"
                             TargetType="{x:Type GroupItem}">
                <Expander IsExpanded="False">
                    <Expander.Header>
                        <StackPanel Orientation="Horizontal">
                            <TextBlock Text="{Binding Path=Name}" />
                        </StackPanel>
                    </Expander.Header>
                    <ItemsPresenter />
                </Expander>
            </ControlTemplate>
            <ControlTemplate x:Key="SingleItemGroupTemplate"
                             TargetType="{x:Type GroupItem}">
                   <ItemsPresenter />
            </ControlTemplate>
        </Style.Resources>
        <Style.Triggers>
            <DataTrigger Binding="{Binding ItemCount}" Value="1">
                <Setter Property="Template"
                        Value="{StaticResource SingleItemGroupTemplate}">
                </Setter>
            </DataTrigger>
        </Style.Triggers>
        <Setter Property="Template"
                Value="{StaticResource MultiItemGroupTemplate}"/>
    </Style>
</GroupStyle.ContainerStyle>

注意 :请注意这会随着 DataGrid 改变... DataGrid 的 ItemPresenter 实际上是 DataGridRowsPresenter

关于WPF Datagrid,是否可以混合使用组和普通(非分组)行?,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/9210627/

10-14 17:08