ObjectListView如何显示子列表<>(ObjectListView how t

2019-10-30 06:18发布

嗨即时通讯使用ObjectListView,且这个类:

public class Person
{
    public string Name{get;set;}
    public List<Things> {get;set;}
}

public class Things
{
    public string Name{get;set;}
    public string Description{get;set;}
}

我怎么能显示出这样的事情在ObjectListView:

Answer 1:

我相信,一个树视图可以帮助在这里。 您可以使用TreeListView分量是ObjectListView的一部分。 这是在使用中非常相似。 你必须提供相关的代表和TLV将做的工作。

我内置AA简单的例子:

当然,还有大量的空间进行定制和改进。

public partial class Form2 : Form {
    public Form2() {
        InitializeComponent();

        // let the OLV know that a person node can expand 
        this.treeListView.CanExpandGetter = delegate(object rowObject) {
            return (rowObject is Person);
        };

        // retrieving the "things" from Person
        this.treeListView.ChildrenGetter = delegate(object rowObject) {
            Person person = rowObject as Person;
            return person.Things;
        };

        // column 1 shows name of person
        olvColumn1.AspectGetter = delegate(object rowObject) {
            if (rowObject is Person) {
                return ((Person)rowObject).Name;
            } else {
                return "";
            }
        };

        // column 2 shows thing information 
        olvColumn2.AspectGetter = delegate(object rowObject) {
            if (rowObject is Thing) {
                Thing thing = rowObject as Thing;
                return thing.Name + ": " + thing.Description;
            } else {
                return "";
            }
        };

        // add one root object and expand
        treeListView.AddObject(new Person("Person 1"));
        treeListView.ExpandAll();
    }
}

public class Person {
    public string Name{get;set;}
    public List<Thing> Things{get;set;}

    public Person(string name) {
        Name = name;
        Things = new List<Thing>();
        Things.Add(new Thing("Thing 1", "Description 1"));
        Things.Add(new Thing("Thing 2", "Description 2"));
    }
}

public class Thing {
    public string Name{get;set;}
    public string Description{get;set;}

    public Thing(string name, string desc) {
        Name = name;
        Description = desc;
    }
}

酒店除了提供的代码,你显然需要添加TLV你形成并添加两列。



文章来源: ObjectListView how to show child list<>