c# - 在 WPF DataGrid 中合并单元格

标签 c# wpf datagrid

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

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

下面的代码怎么改才能搞定?

<DataGrid AutoGenerateColumns="False">
     <DataGrid.Columns>
          <DataGridTextColumn Header="Name" />
          <DataGridTextColumn Header="Attributes" />
     </DataGrid.Columns>
</DataGrid>

最佳答案

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

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 中的数据网格

<DataGrid AutoGenerateColumns="False"  Name="dataGrid1" ItemsSource="{Binding}">
        <DataGrid.Columns>
            <DataGridTemplateColumn Header="Name"  >
                <DataGridTemplateColumn.CellTemplate>
                    <DataTemplate>
                        <Grid>
                            <TextBlock Text="{Binding Path=Name}"  VerticalAlignment="Center" Margin="3,3,3,3"/>
                        </Grid>
                    </DataTemplate>
                </DataGridTemplateColumn.CellTemplate>
            </DataGridTemplateColumn>
            <DataGridTemplateColumn Header="Attributes"  >
                <DataGridTemplateColumn.CellTemplate>
                    <DataTemplate >
                        <Grid>
                            <Grid.RowDefinitions>
                                <RowDefinition Height="50*"/>
                                <RowDefinition />
                                <RowDefinition Height="50*"/>
                            </Grid.RowDefinitions>
                            <TextBlock Grid.Row="0" Text="{Binding Path=Attributes.Attribute1}" VerticalAlignment="Center" Margin="3,3,3,3"/>
                            <Line Grid.Row="1" Stroke="Black" Stretch="Fill" X2="1" VerticalAlignment="Center"/>
                            <TextBlock Grid.Row="2" Text="{Binding Path=Attributes.Attribute2}"  VerticalAlignment="Center" Margin="3,3,3,3"/>
                        </Grid>
                    </DataTemplate>
                </DataGridTemplateColumn.CellTemplate>
            </DataGridTemplateColumn>
        </DataGrid.Columns>
    </DataGrid>

并在code-behind中填写

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

关于c# - 在 WPF DataGrid 中合并单元格,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17202389/

相关文章:

c# - 如何在 .NET 3.5 中从 .NET 4 功能实现 Barrier 类

c# - LINQ 选择一列中具有 Max 且另一列中具有 Distinct 的行

c# - Azure 表 Controller - 通过参数获取记录

wpf - 有什么方法可以声明 WPF 元素的路由策略吗?

wpf - 将 Image 列添加到 WPF 数据网格并根据另一列中的前两个字符进行填充

c# - 检查 Datagrid 中 ItemTamplate 标签的值

c# - 为集换式纸牌游戏加载超过一百万张图像的有效方法是什么?

.net - 在命令绑定(bind)中使用 WPF 控件自己的属性

wpf - 绑定(bind) UserControl 依赖属性和 MVVM

c# - 添加并显示从实体计算并自动更新的元素