如何在WPF滚动保留的DataGrid组头? [英] How to retain DataGrid group headers from scrolling in WPF?

查看:165
本文介绍了如何在WPF滚动保留的DataGrid组头?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

的DataGrid 是充满了许多条目,出现的垂直滚动条,我不想让的DataGrid 滚动查看器隐藏组头。相反,我想有一个滚动条每一的。在我的情况下,总会有只有两个(2)组,所以会有0-2滚动条

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.

下面是一个简约的示例代码:的 http://www.wpftutorial.net/datagrid.html#grouping

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

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



XAML:

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>



甚至在基本的例子出现问题。我想我需要使用的ScrollViewer 地方?

推荐答案

更改您的XAML为以下内容:

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>



您还需要在DataGrid滚动条。在情况下,你组超过展开后可用HIGHT

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

结果是这样的:

< IMG SRC =http://i.stack.imgur.com/PPpi2.pngALT =滚动组>

这篇关于如何在WPF滚动保留的DataGrid组头?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆