C#/WPF:DataGrid 工具包 - 转置行和列

发布于 2024-08-05 21:11:18 字数 1272 浏览 3 评论 0原文

我有一个

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 for “Value1”作为列标题,Value1 的值作为行等。

感谢您的帮助!

干杯

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

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

编辑2: 我在这里还发现了一个很好的方法: http://codemaverick.blogspot.com/2008/02 /transpose-datagrid-or-gridview-by.html

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

千寻… 2024-08-12 21:11:18

您可以像这样使用 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< /code> 属性也显示为一列。

编辑

我现在明白您想要转置DataGrid。我认为简单但有点“hacky”的解决方案是创建一个“转置”对象的列表。为此,您必须提前知道原始列表中有多少个对象,然后创建一个具有与对象一样多的属性的新类。

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 是一个复杂的控件,修改控件模板可能有点让人不知所措。

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.

梦中楼上月下 2024-08-12 21:11:18

将业务对象列表转换为数据表,您已在其中根据对象的属性创建了列,并将其设置为数据网格的项目源。如果要启用编辑,则必须迭代数据表并手动将值应用回业务对象。反射可能有助于使网格变得通用。只是一些想法。

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.

谁的年少不轻狂 2024-08-12 21:11:18

我认为更干净的方法是针对您的场景使用 ItemsControl 而不是 DataGrid

按照此博文上的说明创建您自己的< code>ItemTemplate 以实现最大程度的控制。这样,您可以为每列创建一个模板(可以是数据网格本身)。

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文