Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/287.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 如何将不同值的多维数组绑定到datagrid_C#_Wpf_Wpfdatagrid - Fatal编程技术网

C# 如何将不同值的多维数组绑定到datagrid

C# 如何将不同值的多维数组绑定到datagrid,c#,wpf,wpfdatagrid,C#,Wpf,Wpfdatagrid,我的应用程序中有一个datagrid,我想将bool项的多维数组绑定到该网格 如何将以下项绑定到datagrid 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

我的应用程序中有一个datagrid,我想将bool项的多维数组绑定到该网格

如何将以下项绑定到datagrid 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;
        }
      }
     }

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" />
结果:


如果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" />
结果:


以下代码矩阵[i,0]、[i,1]、[i,2]仅填充第一行。。如果我想填充第二行和其他行,那么条件是什么??我必须使用另一个语句吗?@user1221765代码带变量
I
矩阵中精确移动数据。因此,如果要填充第二行和其他行,首先应该在
矩阵中填充数据。再次检查我的答案。以下代码矩阵[i,0]、[i,1]、[i,2]仅填充第一行。。如果我想填充第二行和其他行,那么条件是什么??我必须使用另一个语句吗?@user1221765代码带变量
I
矩阵中精确移动数据。因此,如果要填充第二行和其他行,首先应该在
矩阵中填充数据。再次检查我的答案。