垂直分组 - WPF DataGrid 或 ListView

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/23988232/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-13 11:49:10  来源:igfitidea点击:

Vertical Grouping - WPF DataGrid or ListView

c#wpfxamldata-binding

提问by Rikki

How can I have the below view by using WPF or creating a custom control?

如何通过使用 WPF 或创建自定义控件来获得以下视图?

DatGrid

数据网格

As I need to use data templates and the cell values might be object instances, I cannot use WinForms to use the old structure. (Not to mention that even if I could I wouldn't!)

由于我需要使用数据模板并且单元格值可能是对象实例,因此我无法使用 WinForms 来使用旧结构。(更不用说即使我可以我也不会!)

Grouping level can be one (like the picture) or more. Four steps is satisfactory here.

分组级别可以是一个(如图所示)或多个。四个步骤在这里是令人满意的。

Any other solutions would be appreciated.

任何其他解决方案将不胜感激。

回答by pushpraj

Here you go

干得好

I defined an ItemsControl bound to Items (your data) and defined a group style to show data as your expectation.

我定义了一个绑定到 Items(您的数据)的 ItemsControl 并定义了一个组样式以按照您的期望显示数据。

    <ItemsControl ItemsSource="{Binding Items}">
        <ItemsControl.GroupStyle>
            <GroupStyle>
                <GroupStyle.ContainerStyle>
                    <Style TargetType="{x:Type GroupItem}">
                        <Setter Property="Template">
                            <Setter.Value>
                                <ControlTemplate TargetType="{x:Type GroupItem}">
                                    <Grid>
                                        <Grid.ColumnDefinitions>
                                            <ColumnDefinition Width="auto" />
                                            <ColumnDefinition />
                                        </Grid.ColumnDefinitions>
                                        <Border BorderBrush="Black" BorderThickness=".5" Padding="4">
                                            <TextBlock Text="{Binding Name}" VerticalAlignment="Center" />
                                        </Border>
                                        <ItemsPresenter Grid.Column="1" />
                                    </Grid>
                                </ControlTemplate>
                            </Setter.Value>
                        </Setter>
                    </Style>
                </GroupStyle.ContainerStyle>
            </GroupStyle>
        </ItemsControl.GroupStyle>
        <ItemsControl.ItemTemplate>
            <DataTemplate>
                <Border BorderBrush="Black" BorderThickness=".5" Padding="4">
                    <TextBlock Text="{Binding Data}" />
                </Border>
            </DataTemplate>
        </ItemsControl.ItemTemplate>
    </ItemsControl>

here is the code to prepare the groups

这是准备组的代码

        Items = new ObservableCollection<Item>();
        Items.Add(new Item() { Key = "abcd", Data = 1 });
        Items.Add(new Item() { Key = "abcd", Data = 2 });
        Items.Add(new Item() { Key = "qwer", Data = 1 });
        Items.Add(new Item() { Key = "qwer", Data = 2 });

        CollectionView view = (CollectionView)CollectionViewSource.GetDefaultView(Items);
        PropertyGroupDescription groupDescription = new PropertyGroupDescription("Key");
        view.GroupDescriptions.Add(groupDescription);

after this leave everything to WPF and enjoy the power of styling and binding

在此之后,将一切交给 WPF 并享受造型和绑定的力量

Multilevel Grouping

多级分组

to achieve miltilevel grouping you simply need to add the PropertyGroupDescription to view.GroupDescriptions

要实现多级分组,您只需将 PropertyGroupDescription 添加到 view.GroupDescriptions

eg

例如

groupDescription = new PropertyGroupDescription("Key2");
view.GroupDescriptions.Add(groupDescription);

Multilevel group sample

多级组样本

there is no limit of sub group you can create, all you need is a key to group.

您可以创建的子组没有限制,您只需要一个组键即可。