0

嗨,我正在使用 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 中显示类似的内容:

在此处输入图像描述

4

1 回答 1

3

我相信树视图可以在这里提供帮助。您可以使用作为 ObjectListView 一部分的 TreeListView 组件。它在使用上非常相似。您必须提供相关代表,TLV 将完成这项工作。

我建立了一个简单的例子:

在此处输入图像描述

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

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 添加到表单并添加两列。

于 2013-10-07T07:39:58.033 回答