C#/WPF:工具包 DataGrid - 转置行和列 [英] C#/WPF: Toolkit DataGrid - Transpose rows and columns

查看:87
本文介绍了C#/WPF:工具包 DataGrid - 转置行和列的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个

List<DetailObject> someList;

看起来像这样:

    public class DetailObject
    {
        public string Titel { get; set; }
        public int Value1 { get; set; }
        public int Value2 { get; set; }
        public int Value3 { get; set; }
    }

有谁知道我如何使用(使用 DataGrid.AutoGenerateColumns="True")'string Titel' 的值作为 RowHeader 和其他成员作为Row"内容?没有任何修改,它会向我显示Titel"作为 ColumnHeader 和 Titel 的值作为行,dito forValue1"作为ColumnHeader,Value1的值作为Rows等

Does anyone know how I can use (with DataGrid.AutoGenerateColumns="True") the value of 'string Titel' as RowHeader and other members as "Row" Content? Without any modifications, it'll show me "Titel" as ColumnHeader and the value of Titel as row, dito for "Value1" as ColumnHeader and the value(s) of Value1 as Rows etc.

感谢您的帮助!

干杯

为了更好地理解,这就是我所拥有的

For better understanding, this is what I have

[Titel]       [Value1]       [Value2]       [Value3]
[Item1.Titel] [Item1.Value1] [Item1.Value2] [Item1.Value3] 
[Item2.Titel] [Item2.Value1] [Item2.Value2] [Item2.Value3] 
[Item3.Titel] [Item3.Value1] [Item3.Value2] [Item3.Value3] 

这就是我要找的:

[Item1.Titel]  [Item2.Titel]  [Item3.Titel] 
[Item1.Value1] [Item2.Value1] [Item3.Value1]
[Item1.Value2] [Item2.Value2] [Item3.Value2]
[Item1.Value3] [Item2.Value3] [Item3.Value3]

我在这里也找到了一个不错的方法:http://codemaverick.blogspot.com/2008/02/transpose-datagrid-or-gridview-by.html

I found also a nice approach here: http://codemaverick.blogspot.com/2008/02/transpose-datagrid-or-gridview-by.html

推荐答案

你可以像这样使用RowHeaderTemplate:

<toolkit:DataGrid>
  <toolkit:DataGrid.RowHeaderTemplate>
    <DataTemplate>
      <TextBlock Text="{Binding Item.Titel, RelativeSource={RelativeSource Mode=FindAncestor, AncestorType={x:Type toolkit:DataGridRow}}}"/>
    </DataTemplate>
  </toolkit:DataGrid.RowHeaderTemplate>
</toolkit:DataGrid>

您还必须将 AutoGenerateColumns 设置为 false 并创建您自己的列以避免 Titel 属性也显示为列.

You will also have to set AutoGenerateColumns to false and create your own columns to avoid the Titel property also being displayed as a column.

编辑

我现在了解到您想要转置 DataGrid.我认为简单但有点hacky"的解决方案是创建一个转置"对象列表.为此,您必须事先知道原始列表中有多少对象,然后创建一个具有与对象一样多的属性的新类.

I now understand that you want to transpose the DataGrid. I think the easy but somewhat "hacky" solution is to create a list of "transposed" objects. To do this you would have to know in advance how many objects there are in the original list and then create a new class with as many properties as there are objects.

class TransposedDetailObject {
  public String Column1 { get; set; }
  public String Column2 { get; set; }
  public String Column3 { get; set; }
}

var transposedList new List<TransposedDetailObject> {
  new TransposedDetailObject {
    Column1 = someList[0].Titel,
    Column2 = someList[2].Titel,
    Column3 = someList[3].Titel
  },
  new TransposedDetailObject {
    Column1 = someList[0].Value1,
    Column2 = someList[2].Value1,
    Column3 = someList[3].Value1
  },
  ...
};

一个不那么hacky"的解决方案是修改DataGrid 的控件模板来交换行和列.但是,DataGrid 是一个复杂的控件,修改控件模板可能有点麻烦.

A less "hacky" solution is to modify the control template of the DataGrid to swap rows and columns. However, DataGrid is a complex control and it can be a bit overwhelming to modify the control template.

这篇关于C#/WPF:工具包 DataGrid - 转置行和列的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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