C#/WPF: Toolkit DataGrid - Transpose rows and colu

2019-01-14 19:05发布

问题:

I've a

List<DetailObject> someList;

which looks like this:

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

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.

Thanks for any help!

Cheers

EDIT: 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: I found also a nice approach here: http://codemaverick.blogspot.com/2008/02/transpose-datagrid-or-gridview-by.html

回答1:

You can use RowHeaderTemplate like this:

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

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.

Edit

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

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.



回答2:

This thread is a bit old but maybe someone is still interested ... I was also looking for way to transpose a WPF DataGrid and finally I found the following open source project on CodePlex:

Transposed WPF DataGrid

Hope this helps.



回答3:

Convert the list of business objects to a datatable where you have created the columns from your object's properties and set that as your datagrid's itemsource. If you want to enable editing, then you'll have to iterate the datatable and manually apply the values back to your business objects. reflection may help make the grid generic. just some thoughts.



回答4:

I think more clean approach is to use ItemsControl instead of DataGrid for your scenario.

Follow the directions on this blog post and create your own ItemTemplate for maximum control. This way, you can create a template for each column (which can be a Datagrid itself).