How can I display array elements in a WPF DataGrid? How can I display array elements in a WPF DataGrid? wpf wpf

How can I display array elements in a WPF DataGrid?


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)

A screenshot of a grid with checkboxes...


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.