【问题标题】:Binding a 2 dimensional bool array to datagrid in wpf将二维布尔数组绑定到 wpf 中的数据网格
【发布时间】: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


【解决方案1】:

你的班级会喜欢这样...

public class Clsdatasource
{
    public bool Str1 { get; set; }
    public bool Str2 { get; set; }
    public bool Str3 { get; set; }
    public Clsdatasource(){}
    public Clsdatasource(bool s1, bool s2, bool s3)
    {
        Str1 = s1;
        Str2 = s2;
        Str3 = s3;
    }
}

你的那些集合看起来像这样......

public class ClsdataSourceCollection : ObservableCollection<Clsdatasource>
{
    private const string FileName = "MyData.xml";
    private readonly XmlSerializer _serializer = new XmlSerializer(typeof(List<Clsdatasource>));
    public void LoadData(Action onCompleted)
    {
        using (StreamReader sr = new StreamReader(FileName))
        {
            var s = _serializer.Deserialize(sr) as List<Clsdatasource>;
            if (s != null)
            {
                Clear();
                s.ForEach(Add);
            }
        }
        onCompleted();
    }
    public void SaveData(Action onCompleted)
    {
        using (StreamWriter sw = new StreamWriter(FileName))
        {
            List<Clsdatasource> tosave = new List<Clsdatasource>();
            tosave.AddRange(this);
            _serializer.Serialize(sw, tosave);
        }
        onCompleted();
    }
}

你可以看到这个 sn-p 发生了什么......

private static void TestSaving()
{
    ClsdataSourceCollection collection = new ClsdataSourceCollection();
    for (int i = 0; i < 100; i++)
    {
        collection.Add(new Clsdatasource(true, true, true));
    }
    collection.SaveData(()=> Console.WriteLine("Saved"));
}
private static void TestLoading()
{
    ClsdataSourceCollection collection = new ClsdataSourceCollection();
    collection.LoadData(() => Console.WriteLine("Loaded"));
}

这两个方法只是创建、保存和加载。工件是应用程序根目录中名为 MyData.xml 的 XML 文件。您需要在所有极端情况以及错误检测和竞赛中编写代码。

整个shebang的最后一步是设置this.dg1.ItemsSource = collection;

请记住,如果您想要实时更新网格,Clsdatasource 类必须从 INotifyPropertyChanged 继承,这完全是一个不同的问题。

这应该让您开始使用序列化。它是“开箱即用”的,因为大约 .NET 3 左右......

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2021-06-18
    • 2011-07-19
    • 2012-02-22
    • 1970-01-01
    • 1970-01-01
    • 2015-08-07
    • 2019-02-09
    • 2011-01-31
    相关资源
    最近更新 更多