我正在使用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中显示类似的内容:
答案 0 :(得分:3)
我相信树视图可以帮到这里。您可以使用TreeListView组件,它是ObjectListView的一部分。它在使用上非常相似。您必须提供相关代表,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添加到表单并添加两列。