ObjectListView 如何显示子列表<> [英] ObjectListView how to show child list&lt;&gt;

查看:19
本文介绍了ObjectListView 如何显示子列表<>的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用 ObjectListView,并拥有这些类:

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

如何在 ObjectListView 中显示类似的内容:

How can I show something like this in the ObjectListView:

推荐答案

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

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

除了提供的代码之外,您显然还必须将 TLV 添加到表单中并添加两列.

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

这篇关于ObjectListView 如何显示子列表<>的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆