简体   繁体   English

垂直分组 - WPF DataGrid或ListView

[英]Vertical Grouping - WPF DataGrid or ListView

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. 由于我需要使用数据模板,并且单元格值可能是对象实例,我不能使用WinForms来使用旧结构。 (Not to mention that even if I could I wouldn't!) (更不用说即使我可以,我也不会!)

Grouping level can be one (like the picture) or more. 分组级别可以是一个(如图片)或更多。 Four steps is satisfactory here. 这里有四个步骤令人满意。

Any other solutions would be appreciated. 任何其他解决方案将不胜感激。

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 要实现miltilevel分组,您只需要将PropertyGroupDescription添加到view.GroupDescriptions

eg 例如

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

多级组样本

there is no limit of sub group you can create, all you need is a key to group. 您可以创建的子组没有限制,您所需要的只是组的关键。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM