如何在WPF DataGrid中显示数组元素? [英] How can I display array elements in a WPF DataGrid?

查看:182
本文介绍了如何在WPF DataGrid中显示数组元素?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试在WPF DataGrid中显示一系列行,其中每行包含一组布尔数组(其数量与所有行的数量相同,不是锯齿形的2D数组),我想显示为个别列,例如。

 名称|第1天|第2天|第3天|第4天|第5天|第6天| 
---------------------------------------------- -------------------
取出垃圾桶X | | X | | | X |
支付账单| | | | | X | |
提交自杀| | | | | | X |

目前,我正在为DataGrid行使用此类:

  private class GridRow {
public string Name {get;组; }
public char Day1 {get;组; }
public char Day2 {get;组; }
public char Day3 {get;组; }
public char Day4 {get;组; }
public char Day5 {get;组; }
public char Day6 {get;组; }
public char Day7 {get;组; }
public char Day8 {get;组; }
public char Day9 {get;组; }
}

在现实世界的情况下,使128个布尔值。它暂时完成工作(只要没有人创建128天的循环计划),但这是一个相当难看的解决方案。



我可以不知何故将一组布尔值提供给DataGrid?我已经看过有关实现ValueConverters的各种文章,但我不知道这是我需要的。

解决方案

我认为你必须处理这个代码...



示例:



测试类:

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

public TestRow(int seed)
{
随机随机=新的随机(种子); (int i = 0; i< Values.Length; i ++)
{
值[i] = random.Next(0,2)== 0假:真
}
}
}

如何生成测试数据;列:

  DataGrid grid = new DataGrid(); 
var data = new TestRow [] {new TestRow(1),new TestRow(2),new TestRow(3)};
grid.AutoGenerateColumns = false; (int i = 0; i< data [0] .Values.Length; i ++)
{
var col = new DataGridCheckBoxColumn();

//这里我绑定到各种索引。
var binding = new Binding(Values [+ i +]);
col.Binding = binding;
grid.Columns.Add(col);
}
grid.ItemsSource = data;

看起来像什么(缺少标题和一切当然)








编辑:为了使上述代码更清洁,您应该在创建时统一使用的items-class中公开一个静态属性使用 data [0] .Values.Length 的数组,如果数据收集为空,则可能会引发异常。


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.

解决方案

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

Example:

Test class:

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)


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.

这篇关于如何在WPF DataGrid中显示数组元素?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆