1

我的应用程序中有一个数据网格,我想将布尔项的多维数组绑定到网格。

如何将以下项目绑定到数据网格 ItemsSource ?

例如

        bool[,] matrix = new bool[10, 22];

        matrix[0, 1] = true;
        matrix[0, 2] = false;
        matrix[0, 3] = true;
        matrix[1, 1] = false;
        matrix[1, 3] = true;

我已经尝试过,我看到一个空的数据网格

 public MatrixPage()
    {
        InitializeComponent();
        bool[,] matrix = new bool[5, 5];

        matrix[0, 1] = true;
        matrix[0, 2] = false;            
        matrix[0, 3] = true;

        var datsource = (from i in Enumerable.Range(0, matrix.GetLength(0))
                         select new clsdatasource(matrix[0, 1], matrix[0, 2], matrix[0, 3])).ToList();

        Matrix_datagrid.ItemsSource = datsource;
    }

    public class clsdatasource
    {
        public bool str1 { get; set; }
        public bool str2 { get; set; }
        public bool str3 { get; set; }

        public clsdatasource(bool s1, bool s2, bool s3)
        {
            this.str1 = s1;
            this.str2 = s2;
            this.str3 = s3;
        }
      }
     }
4

1 回答 1

1

您在 linq 表达式中有错误,您应该使用i变量而不是0

var datsource = (from i in Enumerable.Range(0, matrix.GetLength(0))
                    select new clsdatasource(matrix[i, 1], matrix[i, 2], matrix[i, 3])).ToList();

在 xaml 中,我只有以下代码,并且一切运行良好。

<DataGrid  Name="Matrix_datagrid" />

例子:

bool[,] matrix = new bool[5, 5];

matrix[0, 1] = true;
matrix[0, 2] = false;
matrix[0, 3] = true;
matrix[1, 1] = true;
matrix[2, 2] = true;

结果:

在此处输入图像描述

于 2013-06-04T17:50:05.930 回答