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

查看:1486
本文介绍了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 =真)作为的rowHeader和其他成员行内容的字符串影片名称的价值?无需做任何修改,它会告诉我影片名称为的columnHeader和影片名称为价值排,DITO
值1为的columnHeader和行等值1的值(S)。

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]

,这就是我要找的:

and this is what I'm looking for:

[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:
我还发现一个很好的方法在这里:
<一href=\"http://$c$cmaverick.blogspot.com/2008/02/transpose-datagrid-or-gridview-by.html\">http://$c$cmaverick.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 并创建自己的列,以避免 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
  },
  ...
};

一个不那么哈克的解决方案是修改的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天全站免登陆