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 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您可以像这样使用
RowHeaderTemplate
:您还必须将
AutoGenerateColumns
设置为false
并创建自己的列以避免Titel< /code> 属性也显示为一列。
编辑
我现在明白您想要转置
DataGrid
。我认为简单但有点“hacky”的解决方案是创建一个“转置”对象的列表。为此,您必须提前知道原始列表中有多少个对象,然后创建一个具有与对象一样多的属性的新类。一个不太“hacky”的解决方案是修改 DataGrid 的控件模板以交换行和列。然而,
DataGrid
是一个复杂的控件,修改控件模板可能有点让人不知所措。You can use
RowHeaderTemplate
like this:You will also have to set
AutoGenerateColumns
tofalse
and create your own columns to avoid theTitel
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.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.将业务对象列表转换为数据表,您已在其中根据对象的属性创建了列,并将其设置为数据网格的项目源。如果要启用编辑,则必须迭代数据表并手动将值应用回业务对象。反射可能有助于使网格变得通用。只是一些想法。
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.
我认为更干净的方法是针对您的场景使用
ItemsControl
而不是DataGrid
。按照此博文上的说明创建您自己的< code>ItemTemplate 以实现最大程度的控制。这样,您可以为每列创建一个模板(可以是数据网格本身)。
I think more clean approach is to use
ItemsControl
instead ofDataGrid
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).