Tower
Tower

Reputation: 102945

How to retain DataGrid group headers from scrolling in WPF?

When a DataGrid is filled with many entries so that the vertical scrollbar appears, I don't want the DataGrid scroll viewer to hide the group headers. Instead, I want to have a ScrollBar per each group. In my case, there will always be just two (2) groups, so there will be 0-2 scrollbars.

Here's a minimalistic sample code: http://www.wpftutorial.net/datagrid.html#grouping

Customers = new ListCollectionView(_customers);
Customers.GroupDescriptions.Add(new PropertyGroupDescription("Gender"));

XAML:

<DataGrid ItemsSource="{Binding GroupedCustomers}">
    <DataGrid.GroupStyle>
        <GroupStyle>
            <GroupStyle.HeaderTemplate>
                <DataTemplate>
                    <StackPanel>
                        <TextBlock Text="{Binding Path=Name}" />
                    </StackPanel>
                </DataTemplate>
            </GroupStyle.HeaderTemplate>
            <GroupStyle.ContainerStyle>
                <Style TargetType="{x:Type GroupItem}">
                    <Setter Property="Template">
                        <Setter.Value>
                            <ControlTemplate TargetType="{x:Type GroupItem}">
                                <Expander>
                                    <Expander.Header>
                                        <StackPanel Orientation="Horizontal">
                                          <TextBlock Text="{Binding Path=Name}" />
                                          <TextBlock Text="{Binding Path=ItemCount}"/>
                                          <TextBlock Text="Items"/>
                                        </StackPanel>
                                    </Expander.Header>
                                    <ItemsPresenter />
                                </Expander>
                            </ControlTemplate>
                        </Setter.Value>
                    </Setter>
                </Style>
            </GroupStyle.ContainerStyle>
        </GroupStyle>
    </DataGrid.GroupStyle>
</DataGrid>

The problem occurs even in that basic example. I guess I need to use ScrollViewer somewhere?

Upvotes: 2

Views: 5110

Answers (2)

Mohammed A. Fadil
Mohammed A. Fadil

Reputation: 9387

Change your XAML to the following:

<DataGrid ItemsSource="{Binding GroupedCustomers}">
<DataGrid.GroupStyle>
    <GroupStyle>
        <GroupStyle.HeaderTemplate>
            <DataTemplate>
                <StackPanel>
                    <TextBlock Text="{Binding Path=Name}" />
                </StackPanel>
            </DataTemplate>
        </GroupStyle.HeaderTemplate>
        <GroupStyle.ContainerStyle>
            <Style TargetType="{x:Type GroupItem}">
                <Setter Property="Template">
                    <Setter.Value>
                        <ControlTemplate TargetType="{x:Type GroupItem}">
                            <Expander>
                                <Expander.Header>
                                    <StackPanel Orientation="Horizontal">
                                      <TextBlock Text="{Binding Path=Name}" />
                                      <TextBlock Text="{Binding Path=ItemCount}"/>
                                      <TextBlock Text="Items"/>
                                    </StackPanel>
                                </Expander.Header>

                                <ScrollViewer Height="100">
                                     <ItemsPresenter/>
                                </ScrollViewer>

                            </Expander>
                        </ControlTemplate>
                    </Setter.Value>
                </Setter>
            </Style>
        </GroupStyle.ContainerStyle>
    </GroupStyle>
</DataGrid.GroupStyle>

You still need the DataGrid ScrollBar in-case your groups exceeded the available hight when expanded.

the result is something like this:

Scroll Groups

Upvotes: 5

LPL
LPL

Reputation: 17083

Use the ScrollViewer to wrap the ItemsPresenter in Expander:

<ScrollViewer>
    <ItemsPresenter />
</ScrollViewer>

Maybe you have to disable the VerticalScrollBar for DataGrid:

<DataGrid VerticalScrollBarVisibility="Disabled" />

Upvotes: 1

Related Questions