[英]how to bind multidimensional array of different values to a datagrid
我的应用程序中有一个数据网格,我想将布尔项的多维数组绑定到网格。
如何将以下项目绑定到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" />
例:
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;
结果:
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.