垂直分组 - WPF 数据表格或列表视图

4
我如何使用WPF或创建自定义控件来实现以下视图?

DatGrid

由于我需要使用数据模板,而单元格的值可能是对象实例,所以我不能使用WinForms来使用旧的结构。(更不用说即使我能我也不会!)
分组级别可以是一个(如图片所示)或多个。这里满意的是四个步骤。
任何其他解决方案都将不胜感激。

1
http://www.codeproject.com/Tips/620909/How-to-merge-datacells-in-WPF-DataGridView - Dhaval Patel
您提供的示例使用了Windows Forms旧结构。我需要使用xaml和C#将其转换为WPF。 - Rikki
请查看帖子更新。我需要使用DataTemplates和WPF中的所有功能。因此它不能是旧的DataGrid。 - Rikki
1个回答

6

给你

我定义了一个绑定到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>

以下是准备组的代码:

        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);

把一切交给WPF并享受样式和绑定的强大功能。

多级分组

要实现多级分组,只需将PropertyGroupDescription添加到view.GroupDescriptions中即可。

例如:

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

多级分组示例

您可以创建任意数量的子组,只需一个用于分组的键。


如果我在这里有三个分组步骤怎么办? - Rikki
正如您所看到的,帖子描述已更新以要求更多的分组步骤,我认为这将是清晰明了的。谢谢。 - Rikki
1
假设您的平面数据列表中有一个分组键(key2),我已更新我的答案,模板仍然相同,将嵌套相应地。 - pushpraj
伙计,还有一件事要问。你对你提供的样本中的列标题有什么想法吗? - Rikki
1
使用 ListView 并将其视图设置为 GridView 而不是 ItemsControl 是答案,它将帮助您定义列标题。 - pushpraj
显示剩余2条评论

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接