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

查看:253
本文介绍了C#/ WPF:Toolkit 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; }
    }



< )作为RowHeader和其他成员的String Titel的值作为Row内容?没有任何修改,它将显示Titel为ColumnHeader,并将Titel的值作为行,dito为
Value1作为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]

EDIT2:
我发现也是一个不错的方法:
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 。我认为简单但有些黑客的解决方案是创建一个转置对象的列表。为此,您必须提前知道原始列表中有多少对象,然后创建一个具有与对象相同的属性的新类。

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:Toolkit DataGrid - 转置行和列的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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