在WPF DataGrid中合并单元格

我想创建一个跨越一列多行的WPF数据网格。 像这样:

+-------+----------------+ | Name | Attributes | +-------+----------------+ | | Horse Power | | BMW +----------------+ | | Color | +-------+----------------+ | | Weight | | Ford +----------------+ | | Color | +-------+----------------+ 

如何更改以下代码才能完成?

       

尝试使用DataGridTemplateColumn 。 我为数据绑定创建了示例测试类

 public class Test { public Test(string name, string attribute1, string attribute2) { Name = name; Attributes = new Attribute(attribute1, attribute2); } public string Name { get; set; } public Attribute Attributes { get; set; } } public class Attribute { public Attribute(string attribute1, string attribute2) { Attribute1 = attribute1; Attribute2 = attribute2; } public string Attribute1 { get; set; } public string Attribute2 { get; set; } } 

和xaml中的数据网格

                              

并填写代码隐藏

 List list = new List(); //populate list with your data here dataGrid1.DataContext = list; 

我设法实现了一个解决方案,通过三个级别的分组和一个小问题来完成您想要的任务,因为组标题与数据一起滚动。

   

和网格:

                              ...   

关于这个主题的另一篇有用的文章是http://www.mindstick.com/Articles/90371ed3-c0f7-49eb-ba2d-9d1f2c9b14e2/?Grid%20Control%20in%20WPF :

                  

请注意Grid.ColumnSpanGrid.RowSpan属性。