简体   繁体   English

带有组和 VirtualizingPanel.IsVirtualizingWhenGrouping="True" 的 DataGrid

[英]DataGrid with Groups and VirtualizingPanel.IsVirtualizingWhenGrouping=“True”

I'm encountering a weird behavior with a DataGrid that has a GroupDescription, and with the option VirtualizingPanel.IsVirtualizingWhenGrouping="True" on the xaml.我在使用具有 GroupDescription 的 DataGrid 以及 xaml 上的选项 VirtualizingPanel.IsVirtualizingWhenGrouping="True" 时遇到了奇怪的行为。

Here's part of the code:这是代码的一部分:

In the ViewModel:在视图模型中:

if (collectionView.GroupDescriptions == null) return;
collectionView.GroupDescriptions.Add(new PropertyGroupDescription("Type"));

In the xaml在 xaml 中

... ...

<Style x:Key="DataGridGroupHeaderCountStyle" TargetType="{x:Type GroupItem}">
    <Setter Property="Template">
        <Setter.Value>
            <ControlTemplate TargetType="{x:Type GroupItem}">
                <Expander Background="{Binding Path=Tag,
                                               RelativeSource={RelativeSource Mode=FindAncestor,
                                                                              AncestorType=DataGrid}}"
                          BorderBrush="{Binding Path=Tag,
                                                RelativeSource={RelativeSource Mode=FindAncestor,
                                                                               AncestorType=DataGrid}}"
                          Foreground="{Binding Path=Tag,
                                               RelativeSource={RelativeSource Mode=FindAncestor,
                                                                              AncestorType=DataGrid}}"
                          Style="{StaticResource DataGridExpanderGroupStyle}">
                    <Expander.Header>
                        <DockPanel>
                            <TextBlock MinWidth="100"
                                       Margin="5,0,0,0"
                                       HorizontalAlignment="Left"
                                       VerticalAlignment="Center"
                                       Text="{Binding Path=Name}" />
                            <TextBlock HorizontalAlignment="Left"
                                       VerticalAlignment="Center"
                                       Text="{Binding Path=ItemCount}" />
                        </DockPanel>
                    </Expander.Header>
                    <Expander.Content>
                        <ItemsPresenter />
                    </Expander.Content>
                </Expander>
            </ControlTemplate>
        </Setter.Value>
    </Setter>
</Style>

... ...

<DataGrid x:Name="MyDataGrid"
                      Grid.Row="1"
                      Grid.Column="0"
                      Grid.ColumnSpan="2"
                      IsReadOnly="True"
                      AutoGenerateColumns="False"
                      CanUserAddRows="False"
                      CanUserDeleteRows="False"
                      CanUserSortColumns="True"
                      EnableRowVirtualization="True"
                      IsSynchronizedWithCurrentItem="True"
                      ItemsSource="{Binding Path=CollectionView,
                                            Mode=OneWay}"
                      ScrollViewer.IsDeferredScrollingEnabled="True"
                      SelectionChanged="MyDataGridOnSelectionChanged"
                      SelectionMode="Extended"
                      SelectorHelper.AutoScrollIntoView="True"
                      Tag="{Binding Path=ColorsTheme.Border}"

                      VirtualizingPanel.IsVirtualizingWhenGrouping="True">

                <DataGrid.GroupStyle>
                    <GroupStyle ContainerStyle="{StaticResource DataGridGroupHeaderCountStyle}">
                        <GroupStyle.Panel>
                            <ItemsPanelTemplate>
                                <DataGridRowsPresenter />
                            </ItemsPanelTemplate>
                        </GroupStyle.Panel>
                    </GroupStyle>
                </DataGrid.GroupStyle>
                <DataGrid.Columns>

Description of the Problem :问题描述:

For instance, I have let's say 25 000 rows in the datagrid.例如,我假设数据网格中有 25 000 行。 The loading is OK with the virtualization.虚拟化加载没问题。

My problem is when I click on the first (of the two) group to collapse it : I can never see the second group, it disappears !我的问题是当我单击第一个(两个)组将其折叠时:我永远看不到第二个组,它消失了 Does Somebody know why ?有人知道为什么吗? or is this a bug ?或者这是一个错误?

When I'm not using VirtualizingPanel.IsVirtualizingWhenGrouping="True", the loading is very (extremely) slow, but at least, the behaviour of the groups is normal, that means I can collapse the first group, I can see the second coming below.当我不使用 VirtualizingPanel.IsVirtualizingWhenGrouping="True" 时,加载非常(非常)缓慢,但至少,组的行为是正常的,这意味着我可以折叠第一个组,我可以看到第二个以下。

Maybe a bit late, but could help.也许有点晚,但可以提供帮助。 We just ran into this problem and figured out that the DataGridRowsPresenter in the groups that should appear kept their Visibility = Collapsed .我们刚刚遇到了这个问题,并发现应该出现的组中的DataGridRowsPresenter保持其Visibility = Collapsed As a result the group itself would have an ActualHeight of 0 and not render.因此,该组本身的 ActualHeight 将为 0,并且不会呈现。

To solve this, we hacked(?) in this ItemPanel definition:为了解决这个问题,我们在这个 ItemPanel 定义中修改了(?):

<DataGrid.ItemsPanel>
    <ItemsPanelTemplate>
        <DataGridRowsPresenter IsItemsHost="True" Visibility="{Binding RelativeSource={RelativeSource AncestorType={x:Type Expander}}, Path=IsExpanded, Converter={StaticResource BooleanToVisibilityConverter}}"/>
    </ItemsPanelTemplate>
</DataGrid.ItemsPanel>

Basically enforcing Visibility if the group was expanded.如果组被扩展,基本上会强制执行可见性。 This seems to keep the benefits of the row virtualization when grouping and makes sure the groups that were outside the visible range pop into view.这似乎在分组时保留了行虚拟化的好处,并确保可见范围之外的组弹出到视图中。

Seeing that others had the same issue will probably go ahead and post this as a bug to Microsoft with this "hack" as a hint what might be overlooked in the virtualization, grouping and scroll viewer interaction.看到其他人有同样的问题,可能会继续将此作为错误发布给 Microsoft,并通过此“hack”提示可能在虚拟化、分组和滚动查看器交互中被忽略的内容。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM