如何保留DataGrid组头文件在WPF滚动? [英] How to retain DataGrid group headers from scrolling in WPF?
问题描述
当一个 DataGrid
填充了许多条目,使垂直滚动条出现时,我不想要 DataGrid
滚动浏览器隐藏组头。相反,我想要每个组有一个 ScrollBar
。在我的情况下,总是只有两(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:
<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
The problem occurs even in that basic example. I guess I need to use ScrollViewer
somewhere?
推荐答案
更改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 ScrollBar如果您的群组在扩展时超出了可用的高度。
You still need the DataGrid ScrollBar in-case your groups exceeded the available hight when expanded.
结果是这样的:
< img src =https://i.stack.imgur.com/PPpi2.pngalt =滚动组>
这篇关于如何保留DataGrid组头文件在WPF滚动?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!