简体   繁体   English

在WPF中将二维布尔数组绑定到DataGrid

[英]Binding a 2 dimensional bool array to datagrid in wpf

I have to create a Matrix like datagrid of type bool using 2D Array. 我必须使用2D数组创建类似bool的datagrid的矩阵。 I have a set of boolean string that is needed to be evaluated for each cell in the datagrid as follows 我有一组布尔字符串,需要对数据网格中的每个单元进行评估,如下所示

for eg 例如

cell[0,0] = ((server1 || server 2) && server 3) cell [0,0] =((server1 ||服务器2)&&服务器3)

cell[0,1] = ((server1 && server 3) && server 4) cell [0,1] =((服务器1 &&服务器3)&&服务器4)

cell[1,0] = ((server3 && server 2) || server 4) 单元格[1,0] =((server3 && server 2)||服务器4)

the values for server N is Running or Stopped and it is got from the database. 服务器N的值是“正在运行”或“已停止”,并且它是从数据库中获取的。

how to create a 2D matrix datagrid and how to evaluate the boolean string so that the final result is TRUE or FALSE for each datagrid cell. 如何创建2D矩阵数据网格以及如何评估布尔字符串,以使每个数据网格单元的最终结果为TRUE或FALSE。

I had a look at this link 2D array for string and took this as an example, but I dont know where should I call these evaluation strings. 我查看了该链接2D数组的字符串,并以它为例,但是我不知道应该在哪里调用这些评估字符串。 Do I have to store them in an XML file and then call them or is there anyother way to call them.. 我是否必须将它们存储在XML文件中,然后调用它们,或者有其他方法可以调用它们。

What I have tried : 我尝试过的

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 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>

Kindly help.. if the question is not clear to understand, please comment, I will try to explain more clearly 请帮助..如果不清楚不清楚的问题,请发表评论,我会尽力解释得更清楚

Your class would like like this... 您的班级想要这样...

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;
    }
}

And your collection of those would look like this... 您收集的这些内容将如下所示...

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();
    }
}

And you could see what's going on with this snippet... 您可以看到此代码段发生了什么...

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"));
}

Those two methods just do a create, save, and load. 这两种方法只是创建,保存和加载。 The artefact is an XML file in the root app directory called MyData.xml. 工件是根应用程序目录中的XML文件,称为MyData.xml。 You need to code in all the corner cases and error detection and races. 您需要在所有极端情况以及错误检测和竞争中进行编码。

The last step in the whole shebang is to set this.dg1.ItemsSource = collection; 整个shebang的最后一步是设置this.dg1.ItemsSource = collection;

Remember that if you want real-time updates to your grid, the Clsdatasource class has to inherit from INotifyPropertyChanged, which is a different question altogether. 请记住,如果要实时更新网格,则Clsdatasource类必须继承自INotifyPropertyChanged,这是一个完全不同的问题。

That should get you started with serialization. 那应该使您开始进行序列化。 It comes 'out of the box' since about .NET 3 or so.... 自大约.NET 3以来,它“开箱即用”。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM