如何在C#Winforms应用程序中创建这样的MultiColumn树视图?

时间:2012-03-21 10:29:28

标签: c# treeview

Parent         Col1    Col2    Col3  
|
|____ Child    Data1   Data2  Data3
|
|____ Child2   Data1    Data2  Data3

2 个答案:

答案 0 :(得分:15)

使用ObjectListView库,非常强大且易于使用。

以下是一个完整的例子:

1)编译ObjectListView源代码以获得ObjectListView.dll
2)创建一个新的Windows窗体应用程序并添加ObjectListView.dll作为参考
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);
    }
}

结果:

enter image description here

答案 1 :(得分:-3)

http://www.go-mono.com/mono-downloads/download.html ..下载Gtksharp用于你的操作系统并在visual studio atk-sharp.dll,gdk-sharp.dll,glib-sharp.dll,gtk-sharp.dll中添加这些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();
    }
}