WPF DataGrid: IsVirtualizingWhenGrouping="True" not working

3.7k Views Asked by At

I have a DataGrid that has a CollectionViewSource bound to it's ItemSource Property:

<DataGrid Grid.Row="0" RowBackground="#10808080" AlternatingRowBackground="Transparent"
          HorizontalAlignment="Stretch" VerticalAlignment="Stretch"
          ItemsSource="{Binding Source={StaticResource bookingsViewSource}}"
          RowHeight="27"
          VirtualizingPanel.IsVirtualizingWhenGrouping="True"
          VirtualizingPanel.IsContainerVirtualizable="True"
          VirtualizingPanel.ScrollUnit="Item"
          AutoGenerateColumns="False">

    <DataGrid.Columns>
        <DataGridTextColumn Binding="{Binding date, StringFormat=dd.MM.yyyy}" Header="date"/>
        <DataGridTextColumn Binding="{Binding Path=customers.name}" Header="customer"/>
        <DataGridTextColumn Binding="{Binding Path=customers.street}" Header="adress"/>
    </DataGrid.Columns>

    <DataGrid.GroupStyle>
        <GroupStyle>
            <GroupStyle.ContainerStyle>
                <Style TargetType="{x:Type GroupItem}">
                    <Setter Property="Template">
                        <Setter.Value>
                            <ControlTemplate>
                                <Expander Header="{Binding Path=Name}" IsExpanded="True">
                                    <ItemsPresenter />
                                </Expander>
                            </ControlTemplate>
                        </Setter.Value>
                    </Setter>
                </Style>
            </GroupStyle.ContainerStyle>
        </GroupStyle>
    </DataGrid.GroupStyle>
</DataGrid>

bookingsViewSource is defined as

<CollectionViewSource x:Key="bookingsViewSource"
                      d:DesignSource="{d:DesignInstance {x:Type Database:bookings}}">
    <CollectionViewSource.GroupDescriptions>
        <PropertyGroupDescription PropertyName="providerID"/>
    </CollectionViewSource.GroupDescriptions>
</CollectionViewSource>

and get's filled in code behind section. Everything was doing fine fast and smooth without grouping. But when I added grouping <PropertyGroupDescription PropertyName="providerID"/> the DataGrid needs around one minute to load.

In .NET 4.5 there is a new Property called VirtualizingPanel.IsVirtualizingWhenGrouping and I already set this to true but loading time was not decreasing.

I can not figure out why. Any ideas?

1

There are 1 best solutions below

2
On

From the book: MacDonald M. - Pro WPF 4.5 in C#

A number of factors can break UI virtualization, sometimes when you don’t expect it:

  • Putting your list control in a ScrollViewer: The ScrollViewer provides a window onto its child content. The problem is that the child content is given unlimited “virtual” space. In this virtual space, the ListBox renders itself at full size, with all of its child items on display. As a side effect, each item gets its own memory-hogging ListBoxItem object. This problem occurs any time you place a ListBox in a container that doesn’t attempt to constrain its size; for example, the same problem crops up if you pop it into a StackPanel instead of a Grid.
  • Changing the list’s control template and failing to use the ItemsPresenter: The ItemsPresenter uses the ItemsPanelTemplate, which specifies the VirtualizingStackPanel. If you break this relationship or if you change the ItemsPanelTemplate yourself so it doesn’t use a VirtualizingStackPanel, you’ll lose the virtualization feature.
  • Not using data binding: It should be obvious, but if you fill a list programmatically— for example, by dynamically creating the ListBoxItem objects you need—no virtualization will occur. Of course, you can consider using your own optimization strategy, such as creating just those objects that you need and only creating them at the time they’re needed. You’ll see this technique in action with a TreeView that uses just-in-time node creation to fill a directory tree in Chapter 22. If you have a large list, you need to avoid these practices to ensure good performance.

Also, in my case the issue was caused by MahApps.Metro styles.