如何显示在一个WPF DataGrid中的数组元素?数组、元素、WPF、DataGrid

2023-09-02 21:37:11 作者:我知道你爱的不是我

我想显示一系列在WPF的DataGrid的行,每一行包含布尔值(其数量是相同的所有行,它不是一个锯齿形二维数组),我想显示为数组单个列,如:

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   |

目前,我使用这个类为我的DataGrid行:

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

在现实世界中的情况下,作出这样的128布尔。它得到暂时做(只要没有人创建了一个长度超过128天循环计划)的工作,但它是一个比较难看的解决方案。

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.

我可以以某种方式养活布尔数组到DataGrid中?我已经看到了各种各样的文章上实现ValueConverters,但我不知道这就是我所需要的。

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.

推荐答案

我认为你必须处理身后这个code ...

I think you'll have to deal with code behind for this...

例如:

测试类:

public class TestRow
{
    private bool[] _values = new bool[10];
    public bool[] Values
    {
        get { return _values; }
        set { _values = value; }
    }

    public TestRow(int seed)
    {
        Random random = new Random(seed);
        for (int i = 0; i < Values.Length; i++)
        {
            Values[i] = random.Next(0, 2) == 0 ? false : true;
        }
    }
}

如何生成测试数据和放大器;列:

How to generate test data & columns:

DataGrid grid = new DataGrid();
var data = new TestRow[] { new TestRow(1), new TestRow(2), new TestRow(3) };
grid.AutoGenerateColumns = false;
for (int i = 0; i < data[0].Values.Length; i++)
{
    var col = new DataGridCheckBoxColumn();
    //Here i bind to the various indices.
    var binding = new Binding("Values[" + i + "]");
    col.Binding = binding;
    grid.Columns.Add(col);
}
grid.ItemsSource = data;

是什么样子(没有头,当然一切)

What that looks like (lacks headers and everything of course)

编辑:为了使上面的code清洁剂,你应该在你的项目级的公开静态属性即创建阵列时,使用数据[使用统一0] .Values​​.Length 创建列时,能明显抛出一个异常,如果数据收集是空的。

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.