如何在 C# Winforms 应用程序中创建这样的多列树视图?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/9802724/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to create a MultiColumn treeview like this in C# Winforms app?
提问by Aravind Srinivas
Parent Col1 Col2 Col3
|
|____ Child Data1 Data2 Data3
|
|____ Child2 Data1 Data2 Data3
采纳答案by digEmAll
Use the ObjectListViewlibrary, is very powerful and pretty easy to use.
使用ObjectListView库,功能非常强大,也很容易使用。
Here's a full example:
这是一个完整的例子:
1) compile the ObjectListView source code to get a ObjectListView.dll
2) create a new Windows Forms Application and add the ObjectListView.dllas reference
3) Open the Form1.cs code and copy the following code inside:
1) 编译 ObjectListView 源代码得到一个ObjectListView.dll
2) 创建一个新的 Windows Forms Application 并添加ObjectListView.dllas 引用
3) 打开 Form1.cs 代码并将以下代码复制到里面:
public partial class Form1 : Form
{
// embedded class
class Node
{
public string Name { get; private set; }
public string Column1 { get; private set; }
public string Column2 { get; private set; }
public string Column3 { get; private set; }
public List<Node> Children { get; private set; }
public Node(string name, string col1, string col2, string col3)
{
this.Name = name;
this.Column1 = col1;
this.Column2 = col2;
this.Column3 = col3;
this.Children = new List<Node>();
}
}
// private fields
private List<Node> data;
private BrightIdeasSoftware.TreeListView treeListView;
// constructor
public Form1()
{
InitializeComponent();
AddTree();
InitializeData();
FillTree();
}
// private methods
private void FillTree()
{
// set the delegate that the tree uses to know if a node is expandable
this.treeListView.CanExpandGetter = x => (x as Node).Children.Count > 0;
// set the delegate that the tree uses to know the children of a node
this.treeListView.ChildrenGetter = x => (x as Node).Children;
// create the tree columns and set the delegates to print the desired object proerty
var nameCol = new BrightIdeasSoftware.OLVColumn("Name", "Name");
nameCol.AspectGetter = x => (x as Node).Name;
var col1 = new BrightIdeasSoftware.OLVColumn("Column1", "Column1");
col1.AspectGetter = x => (x as Node).Column1;
var col2 = new BrightIdeasSoftware.OLVColumn("Column2", "Column2");
col2.AspectGetter = x => (x as Node).Column2;
var col3 = new BrightIdeasSoftware.OLVColumn("Column3", "Column3");
col3.AspectGetter = x => (x as Node).Column3;
// add the columns to the tree
this.treeListView.Columns.Add(nameCol);
this.treeListView.Columns.Add(col1);
this.treeListView.Columns.Add(col2);
this.treeListView.Columns.Add(col3);
// set the tree roots
this.treeListView.Roots = data;
}
private void InitializeData()
{
// create fake nodes
var parent1 = new Node("PARENT1", "-", "-", "-");
parent1.Children.Add(new Node("CHILD_1_1", "A", "X", "1"));
parent1.Children.Add(new Node("CHILD_1_2", "A", "Y", "2"));
parent1.Children.Add(new Node("CHILD_1_3", "A", "Z", "3"));
var parent2 = new Node("PARENT2", "-", "-", "-");
parent2.Children.Add(new Node("CHILD_2_1", "B", "W", "7"));
parent2.Children.Add(new Node("CHILD_2_2", "B", "Z", "8"));
parent2.Children.Add(new Node("CHILD_2_3", "B", "J", "9"));
var parent3 = new Node("PARENT3", "-", "-", "-");
parent3.Children.Add(new Node("CHILD_3_1", "C", "R", "10"));
parent3.Children.Add(new Node("CHILD_3_2", "C", "T", "12"));
parent3.Children.Add(new Node("CHILD_3_3", "C", "H", "14"));
data = new List<Node> { parent1, parent2, parent3 };
}
private void AddTree()
{
treeListView = new BrightIdeasSoftware.TreeListView();
treeListView.Dock = DockStyle.Fill;
this.Controls.Add(treeListView);
}
}
Result:
结果:


回答by Aravind Srinivas
http://www.go-mono.com/mono-downloads/download.html.. Download Gtksharp for ur operating system and add refrence of these dll in visual studio atk-sharp.dll , gdk-sharp.dll , glib-sharp.dll , gtk-sharp.dll and use using Gtk; ... U will get the TreeView
http://www.go-mono.com/mono-downloads/download.html.. 为您的操作系统下载 Gtksharp 并在 Visual Studio atk-sharp.dll、gdk-sharp.dll、glib- 中添加这些 dll 的引用sharp.dll , gtk-sharp.dll 并使用 Gtk;... 你会得到 TreeView
public class TreeViewExample
{
public static void Main()
{
Gtk.Application.Init();
new TreeViewExample();
Gtk.Application.Run();
}
public TreeViewExample()
{
Gtk.Window window = new Gtk.Window("TreeView Example");
window.SetSizeRequest(500, 200);
Gtk.TreeView tree = new Gtk.TreeView();
window.Add(tree);
Gtk.TreeViewColumn Parent = new Gtk.TreeViewColumn();
Parent.Title = "Parent";
Gtk.CellRendererText Parent1 = new Gtk.CellRendererText();
Parent.PackStart(Parent1, true);
Gtk.TreeViewColumn ChildColoumn1 = new Gtk.TreeViewColumn();
ChildColoumn1.Title = "Column 1";
Gtk.CellRendererText Child1 = new Gtk.CellRendererText();
ChildColoumn1.PackStart(Child1, true);
Gtk.TreeViewColumn ChildColumn2 = new Gtk.TreeViewColumn();
ChildColumn2.Title = "Column 2";
Gtk.CellRendererText Child2 = new Gtk.CellRendererText();
ChildColumn2.PackStart(Child2, true);
tree.AppendColumn(Parent);
tree.AppendColumn(ChildColoumn1);
tree.AppendColumn(ChildColumn2);
Parent.AddAttribute(Parent1, "text", 0);
ChildColoumn1.AddAttribute(Child1, "text", 1);
ChildColumn2.AddAttribute(Child2, "text", 2);
Gtk.TreeStore Tree = new Gtk.TreeStore(typeof(string), typeof(string), typeof(string));
Gtk.TreeIter iter = Tree.AppendValues("Parent1");
Tree.AppendValues(iter, "Child1", "Node 1");
iter = Tree.AppendValues("Parent2");
Tree.AppendValues(iter, "Child1", "Node 1","Node 2");
tree.Model = Tree;
window.ShowAll();
}
}

