简体   繁体   English

创建动态二维数组,根据c#wpf中的列表自动调整

[英]creating dynamic 2d array that automatically adjusts based on list in c# wpf

Suppose I have a list of objects of a C# class. 假设我有一个C#类的对象列表。 I wish to have a 2d array (or list of lists, or something else that can do the same) that can hold information of the relations between all objects in the list. 我希望有一个2d数组(或列表列表,或其他可以做同样的事情),它可以保存列表中所有对象之间关系的信息。 The class could look like this: 这个类看起来像这样:

public class SimplePoint
{
    public string strName{ get; set; }
};

public class SimpleRoute
{
    public int iDistance{ get; set; }
};

public class Place
{
    public ObservableCollection<SimplePoint> points;

    // Below array should dynamically increase/decrease size 
    // depending on length of list of points
    // The array should store 2 values for each possible set of points:
    // distance from x to y, and distance from y to x
    public SimpleRoute[, ] routes;
};

The 2d array/table should have one row and one column for every element in the list. 2d数组/表的列表中的每个元素应具有一行和一列。 When an element is added to the list, the 2d array should grow in two directions, and when an element is deleted, the table/array should shrink in two directions. 将元素添加到列表时,2d数组应在两个方向上增长,并且当删除元素时,表/数组应在两个方向上缩小。

The two dimensional array as declared in the above code will obviously not work for this. 上面代码中声明的二维数组显然不适用于此。 Rather, I am wondering if the 'SimpleRoute' class could somehow abserve the ObservableCollection 'points', and internally maintain the table of distance data. 相反,我想知道'SimpleRoute'类是否能以某种方式保留ObservableCollection'点',并在内部维护距离数据表。

Later I wish to also be able to store the data from the 2d array/table - or from the class behind/around it - in an XML (through serialization) and be able to load it again (through deserialization). 后来我希望能够将来自2d数组/表的数据 - 或者来自其后面/周围的类 - 的数据存储在XML中(通过序列化),并且能够再次加载它(通过反序列化)。 The relation to the list of 'SimplePoints' should be maintained. 与“简单点”列表的关系应保持。 Lastly, the data (ie. the distances) in the automatically adjusting table should editable by the user. 最后,自动调整表中的数据(即距离)应该由用户编辑。 I will create the GUI using WPF. 我将使用WPF创建GUI。

I am wondering what is the best approach for this in C#/.NET, but find it hard to think of something elegant. 我想知道在C#/ .NET中最好的方法是什么,但是很难想到优雅的东西。 Searching hasen't yielded a lot of results, though I may be looking in the wrong place. 虽然我可能在错误的地方寻找,但搜索并没有产生很多结果。

Maybe there is some easy solution? 也许有一些简单的解决方案? I am relatively newto .NET programming. 我相对较新的.NET编程。

I think that you're asking for information on quite a big domain of .net / general OOP practices, so it's really hard to provide a decent answer without a bit of context, but I'll try to give you some pointers. 我认为您是在寻求有关.net /一般OOP惯例的广泛领域的信息,因此,在没有一点上下文的情况下很难提供一个体面的答案,但是我将尽力为您提供一些指导。

I'm assuming you have two classes, say Group and Person, with the following simple definitions: 我假设您有两个类,例如Group和Person,具有以下简单定义:

class Group
{
    public string name { get; set; }
    public List<Person> Persons = new List<Person>();
}
class Person
{
    public string name { get; set; }
    public int age { get; set; }
    public Person(string name, int age)
    {
        this.name = name;
        this.age = age;
    }
}

You have created an object Group with an 1:N relationship to the object Person. 您已经创建了与对象Person具有1:N关系的对象组。 You can test this method with the following test function: 您可以使用以下测试功能来测试此方法:

public static void test()
{
    Group group =new Group();
    group.name= "Friends";

    Person p = new Person("John",30);
    Person p2 = new Person("Rute",25);
    Person p3= new Person("Richard",17);

    group.Persons.Add(p);
    group.Persons.Add(p2);
    group.Persons.Add(p3);

    foreach(Person person in group.Persons)
    {
        Console.WriteLine("{0} is {1} years old. He is in group {2}.",person.name,person.age,group.name);
    }
}

Is this what you intended? 这是你的意图吗? If you want to create a different type of relationship (N:N, N:1), I can modify my code to help you with that too. 如果你想创建一个不同类型的关系(N:N,N:1),我可以修改我的代码来帮助你。

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

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