ObjectListView how to show child list<>

懵懂的女人 提交于 2019-12-13 06:20:24

问题


Hi Im using ObjectListView, and having this classes:

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

How can I show something like this in the ObjectListView:


回答1:


I believe a tree-view can help here. You could use the TreeListView component which is part of the ObjectListView. It is very similar in use. You have to provide the relevant delegates and the TLV will do the work.

I built a a quick example:

Of course, there is lots of room for customization and improvement.

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

Inn addition to the provided code, you obviously have to add the TLV to you form and add two columns.



来源:https://stackoverflow.com/questions/19188245/objectlistview-how-to-show-child-list

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!