合并在WPF的DataGrid细胞 [英] Merge Cells in WPF DataGrid

查看:226
本文介绍了合并在WPF的DataGrid细胞的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

  + ------- + ---------------- +
|名称|属性|
------- + + + ----------------
| |马电源|
| BMW + ---------------- +
| |颜色|
------- + + + ----------------
| |重量|
|福特+ ---------------- +
| |颜色|
------- + + + ----------------

如何能在以下code改为把它做?

 <数据网格的AutoGenerateColumns =FALSE>
     < D​​ataGrid.Columns>
          < D​​ataGridTextColumn标题=名称/>
          < D​​ataGridTextColumn标题=属性/>
     < /DataGrid.Columns>
< / DataGrid的>


解决方案

尝试使用 DataGridTemplateColumn 。我创建的样本测试类绑定

 公共类测试
{    公开测试(字符串名称,字符串ATTRIBUTE1,串attribute2)
    {
        名称=名称;
        属性=新的属性(ATTRIBUTE1,attribute2);
    }    公共字符串名称{;组; }
    公共属性属性{搞定;组; }
}公共类属性
{    公共属性(字符串ATTRIBUTE1,串attribute2)
    {
        ATTRIBUTE1 = ATTRIBUTE1;
        Attribute2 = attribute2;
    }    公共字符串ATTRIBUTE1 {搞定;组; }
    公共字符串Attribute2 {搞定;组; }
}

和在XAML一个DataGrid

 <数据网格的AutoGenerateColumns =假名称=DataGrid1中的ItemsSource ={结合}>
        < D​​ataGrid.Columns>
            < D​​ataGridTemplateColumn标题=名称>
                < D​​ataGridTemplateColumn.CellTemplate>
                    <&DataTemplate的GT;
                        <网格和GT;
                            < TextBlock的文本={绑定路径=名称}VerticalAlignment =中心保证金=3,3,3,3/>
                        < /网格和GT;
                    < / DataTemplate中>
                < /DataGridTemplateColumn.CellTemplate>
            < / DataGridTemplateColumn>
            < D​​ataGridTemplateColumn标题=属性>
                < D​​ataGridTemplateColumn.CellTemplate>
                    <&DataTemplate的GT;
                        <网格和GT;
                            < Grid.RowDefinitions>
                                < RowDefinition HEIGHT =50 */>
                                < RowDefinition />
                                < RowDefinition HEIGHT =50 */>
                            < /Grid.RowDefinitions>
                            < TextBlock的Grid.Row =0文本={绑定路径= Attributes.Attribute1}VerticalAlignment =中心保证金=3,3,3,3/>
                            <线Grid.Row =1行程=黑拉伸=填充X2 =1VerticalAlignment =中心/>
                            < TextBlock的Grid.Row =2文本={绑定路径= Attributes.Attribute2}VerticalAlignment =中心保证金=3,3,3,3/>
                        < /网格和GT;
                    < / DataTemplate中>
                < /DataGridTemplateColumn.CellTemplate>
            < / DataGridTemplateColumn>
        < /DataGrid.Columns>
    < / DataGrid的>

和填写好code-背后

 列表<试验>名单=新名单<试验>();
//填充列表与您的数据在这里
dataGrid1.DataContext =清单;

I want to create a WPF datagrid that spans over multiple rows in one column. Like this:

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

How can the following code be changed to get it done?

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

解决方案

Try use DataGridTemplateColumn. I created sample test class for databinding

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

And a datagrid in 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>

And fill it in code-behind

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

这篇关于合并在WPF的DataGrid细胞的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆