【发布时间】:2013-06-03 08:58:09
【问题描述】:
我必须使用二维数组创建一个类似于 bool 类型的数据网格的矩阵。我有一组布尔字符串,需要对数据网格中的每个单元格进行评估,如下所示
例如
cell[0,0] = ((server1 || server 2) && server 3)
cell[0,1] = ((server1 && server 3) && server 4)
cell[1,0] = ((server3 && server 2) || server 4)
服务器 N 的值是 Running 或 Stopped,它是从数据库中获取的。
如何创建二维矩阵数据网格以及如何评估布尔字符串,以便每个数据网格单元格的最终结果为 TRUE 或 FALSE。
我查看了这个链接2D array for string 并以此为例,但我不知道应该在哪里调用这些评估字符串。我必须将它们存储在 XML 文件中然后调用它们还是有其他方法可以调用它们..
我尝试过的:
public MatrixPage()
{
InitializeComponent();
bool[,] matrixcell = new bool[10, 22];
matrixcell[0, 0] = // should I place the Evaluation string here;
matrixcell[0, 1] = ;
for (int i = 0; i < 10; i++)
{
for (int j = 0; j < 22; j++)
{
matrixcell[i, j] = // or Should I call here the evaluation boolean string for each iteration respective to the row/column from a file like XML or a any other file ??
}
}
var datsource = (from i in Enumerable.Range(0, matrixcell.GetLength(0))
select new clsdatasource(matrixcell[i, 0], matrixcell[i, 1], matrixcell[i,3])).ToList();
this.dg1.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;
}
}
XAML
<Grid>
<DataGrid x:Name="dg1" CanUserAddRows="False" AutoGenerateColumns="False">
<DataGrid.Columns>
<DataGridTextColumn Header="System1" Binding="{Binding str1}"/>
<DataGridTextColumn Header="System2" Binding="{Binding str2}"/>
<DataGridTextColumn Header="System3" Binding="{Binding str3}"/>
</DataGrid.Columns>
</DataGrid>
</Grid>
请帮助..如果问题不清楚,请发表评论,我会尽量解释得更清楚
【问题讨论】:
-
网格是静态的吗?如果不是,它应该从 INotifyPropertyChanged 继承。在演示过程中,布尔值将评估为它们的字符串表示。语言本身不允许从 bool 到 string 的隐式转换,但绑定会自动处理它。哪个方面不工作?
-
@GarryVass 不,我希望动态创建网格。在这里评估布尔字符串不是问题,我不知道将这些布尔字符串放置在每个 [cell] 的位置(我的意思是我应该将它们放置在哪种文件格式中,以便我可以在迭代期间检索它们)。并在检索它们之后如何将它们放置在相应的矩阵单元中
-
好的,这更清楚了,谢谢。如果您需要持久存储,那么您对 XML 的预感将是我的选择。您将需要 clsdatasource 类的无参数构造函数,我会将数据反序列化为 ObservableCollection
。但这仅基于我现在对您的问题的理解。 -
感谢您的回复,如果可能的话,您能否发布一个简单的示例,它是如何工作的。这将是我现在进步的一个真正的开始
-
如果有任何用处,我可以放一些序列化和反序列化数据的伪代码。我不知道关于服务器正在启动的部分以及什么...
标签: c# wpf wpfdatagrid