如何在 WPF DataGrid 中显示数组元素?
我正在尝试在 WPF DataGrid 中显示一系列行,其中每行都包含一个布尔数组(所有行的数量相同,它不是锯齿状的二维数组),我想将其显示为单独的列,例如。
Name | Day 1 | Day 2 | Day 3 | Day 4 | Day 5 | Day 6 |
-----------------------------------------------------------------
Bring out Trash | X | | X | | | X |
Pay Bills | | | | | X | |
Commit Suicide | | | | | | X |
目前,我正在将此类用于我的 DataGrid 行:
private class GridRow {
public string Name { get; set; }
public char Day1 { get; set; }
public char Day2 { get; set; }
public char Day3 { get; set; }
public char Day4 { get; set; }
public char Day5 { get; set; }
public char Day6 { get; set; }
public char Day7 { get; set; }
public char Day8 { get; set; }
public char Day9 { get; set; }
}
在实际情况中,使用 128 个布尔值。它暂时可以完成工作(只要没有人创建长度超过 128 天的循环计划),但这是一个相当丑陋的解决方案。
我可以以某种方式将布尔值数组输入到 DataGrid 中吗?我查看了有关实现 ValueConverters 的各种文章,但我不确定这就是我需要的。
I'm trying to display a series of rows in a WPF DataGrid where each row contains an array of booleans (the number of which is the same for all rows, it's not a jagged 2D array) that I want to display as individual columns, eg.
Name | Day 1 | Day 2 | Day 3 | Day 4 | Day 5 | Day 6 |
-----------------------------------------------------------------
Bring out Trash | X | | X | | | X |
Pay Bills | | | | | X | |
Commit Suicide | | | | | | X |
Currently, I'm using this class for my DataGrid rows:
private class GridRow {
public string Name { get; set; }
public char Day1 { get; set; }
public char Day2 { get; set; }
public char Day3 { get; set; }
public char Day4 { get; set; }
public char Day5 { get; set; }
public char Day6 { get; set; }
public char Day7 { get; set; }
public char Day8 { get; set; }
public char Day9 { get; set; }
}
In the real world case, make that 128 booleans. It gets the job done for the time being (for as long as nobody creates cyclic plans with a length over 128 days) but it's a rather ugly solution.
Can I somehow feed an array of booleans into the DataGrid? I've taken a look various articles on implementing ValueConverters, but I'm not sure that's what I need.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我认为您必须为此处理后面的代码...
示例:
测试类:
如何生成测试数据和测试数据columns:
看起来像什么(当然缺少标题和所有内容)
编辑:为了使上面的代码更清晰,您应该在创建数组时统一使用的项目类中公开一个静态属性,在以下情况下使用
data[0].Values.Length
如果数据集合为空,创建列显然会引发异常。I think you'll have to deal with code behind for this...
Example:
Test class:
How to generate test data & columns:
What that looks like (lacks headers and everything of course)
Edit: To make the above code cleaner you should expose a static property in your items-class which is used uniformly when creating the arrays, using
data[0].Values.Length
when creating the columns could obviously throw an exception if the data collection is empty.