如何在C#的WinForms应用程序一个多树视图这样吗? [英] How to create a MultiColumn treeview like this in C# Winforms app?

查看:787
本文介绍了如何在C#的WinForms应用程序一个多树视图这样吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

 父COL1 col2的COL3 
|
| ____儿童数据1数据2数据3
|
| ____ CHILD2数据1数据2数据3


解决方案

使用 ObjectListView 库,是非常强大的,很容易使用。



下面是一个完整的例子:



1)编译ObjectListView源代码,以获得 ObjectListView.dll 结果
2)创建一个新的Windows窗体应用程序,并添加 ObjectListView.dll 作为参考结果
3)打开Form1.cs的代码和内部复制下面的代码:

 公共部分Form1类:表格
{
//嵌入类
类节点
{
公共字符串名称{;私人集; }
公共字符串列1 {搞定;私人集; }
公共字符串列2 {搞定;私人集; }
公共字符串栏3 {搞定;私人集; }
公开名单<节点>儿童{搞定;私人集; }
公共节点(字符串名称,字符串COL1,COL2字符串,字符串COL3)
{
this.Name =名称;
this.Column1 = COL1;
this.Column2 = COL2;
this.Column3 = COL3;
this.Children =新的List<节点>();
}
}

//私有字段
私有列表<节点>数据;
私人BrightIdeasSoftware.TreeListView treeListView;

//构造
公共Form1中()
{
的InitializeComponent();
AddTree();
InitializeData();
FillTree();
}

//私有方法
私人无效FillTree()
{
//设置树使用要知道如果一个节点是委托扩展
this.treeListView.CanExpandGetter = X => (X为节点).Children.Count> 0;
//设置树使用要知道一个节点
this.treeListView.ChildrenGetter = X =>中孩子委托; (X为节点)。儿童;

//创建树列,并设置委托给打印所需的对象proerty
变种nameCol =新BrightIdeasSoftware.OLVColumn(姓名,姓名);
nameCol.AspectGetter = X => (X为节点).Name点;

变种COL1 =新BrightIdeasSoftware.OLVColumn(列1,列1);
col1.AspectGetter = X => (X为节点).Column1;

变种COL2 =新BrightIdeasSoftware.OLVColumn(列2,列2);
col2.AspectGetter = X => (X为节点).Column2;

变种COL3 =新BrightIdeasSoftware.OLVColumn(栏3,栏3);
col3.AspectGetter = X => (X为节点).Column3;

//添加列到树
this.treeListView.Columns.Add(nameCol);
this.treeListView.Columns.Add(COL1);
this.treeListView.Columns.Add(COL2);
this.treeListView.Columns.Add(COL3);

//设置树根
this.treeListView.Roots =数据;
}

私人无效InitializeData()
{
//创建假的节点
变种parent1 =新节点(PARENT1, - , - , - );
parent1.Children.Add(新节点(CHILD_1_1,A,X,1));
parent1.Children.Add(新节点(CHILD_1_2,A,Y,2));
parent1.Children.Add(新节点(CHILD_1_3,A,Z,3));

变种parent2 =新节点(PARENT2, - , - , - );
parent2.Children.Add(新节点(CHILD_2_1,B,W,7));
parent2.Children.Add(新节点(CHILD_2_2,B,Z,8));
parent2.Children.Add(新节点(CHILD_2_3,B,J,9));

变种parent3 =新节点(PARENT3, - , - , - );
parent3.Children.Add(新节点(CHILD_3_1,C,R,10));
parent3.Children.Add(新节点(CHILD_3_2,C,T,12));
parent3.Children.Add(新节点(CHILD_3_3,C,H,14));

数据=新的List<节点> {parent1,parent2,parent3};
}

私人无效AddTree()
{
treeListView =新BrightIdeasSoftware.TreeListView();
treeListView.Dock = DockStyle.Fill;
this.Controls.Add(treeListView);
}
}



结果:




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

解决方案

Use the ObjectListView library, is very powerful and pretty easy to use.

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.dll as reference
3) Open the Form1.cs code and copy the following code inside:

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:

这篇关于如何在C#的WinForms应用程序一个多树视图这样吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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