14
6月
2020

DevExpress建立一个常规TreeList

XtraTreeList 是DevExpress中的一个树形控件,它可以将信息显示为树状、网格或两者的结合,XtraTreeList 不缓存任何数据,因此只占用少量内存,并且快速执行数据操作,今天我给大家实战演示如何快速建立一个常规TreeList!

首先介绍一下软件的主界面,TreeList中包括学科、作者、ISBN、出版日期和版次等字段,并由两个父节点建立分支,如图1所示:

interface

图1-主界面

1.XtraTreeList添加列

首先建立一个Winform,目标框架选择.Net4.7.2或以上,在界面上添加一个TreeList控件,右键选择“Run Designer”,分别添加学科、作者、ISBN、出版日期和版次的列,如图2所示:

gude1

图2-操作步骤1

2.建立Library类设置属性

我们新建一个Library类用来绑定TreeList,类中声明了子节点ID、父节点ID、学科、作者、ISBN、出版日期和版次等字段,建立相应的属性设置方法。

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace CreateXtraTreeList
{
    class Library
    {

        //子节点ID变量
        private int m_ID = -1;

        //父节点ID变量
        private int m_ParentID = -1;

        // 学科字段变量
        private string m_Subject = string.Empty;

        // 作者字段变量
        private string m_Author = string.Empty;

        // ISBN字段变量
        private string m_ISBN = string.Empty;

        // 出版日期字段变量
        private string m_Pdate = string.Empty;

        // 班次字段变量
        private string m_Version = string.Empty;

        public int ID
        {
            get

            {
                return m_ID;
            }

            set
            {
                m_ID = value;
            }
        }

        public int ParentID
        {
            get
            {
                return m_ParentID;
            }

            set
            {
                m_ParentID = value;
            }
        }


        public string Subject
        {
            get
            {
                return m_Subject;
            }

            set
            {
                m_Subject = value;
            }
        }

        public string Author
        {
            get
            {
                return m_Author;
            }

            set
            {
                m_Author = value;
            }
        }

        public string ISBN
        {
            get
            {
                return m_ISBN;
            }

            set
            {
                m_ISBN = value;
            }
        }

        public string Pdate
        {
            get
            {
                return m_Pdate;
            }

            set
            {
                m_Pdate = value;
            }
        }

        public string Version
        {
            get
            {
                return m_Version;
            }

            set
            {
                m_Version = value;
            }
        }

    }
}

3.TreeList绑定数据

双击主界面生成FormLoad函数,在函数中完成字段赋值和TreeList的数据绑定。

private void Form1_Load(object sender, EventArgs e)
       {
           ArrayList BookList = new ArrayList();//声明一个ArrayList存放图书信息

           Library m_catefory1 = new Library();
           m_catefory1.ID = 1;
           m_catefory1.Subject = "计算机科学与技术";          
           BookList.Add(m_catefory1);

           Library m_book1 = new Library();
           m_book1.ID = 2;//子节点2
           m_book1.ParentID = 1;//隶属父节点1
           m_book1.Subject = "C#设计模式";
           m_book1.Author = "(美)James W. Cooper";
           m_book1.ISBN = "9787030308153";
           m_book1.Pdate = "2011-07";
           m_book1.Version = "1";
           BookList.Add(m_book1);

           Library m_book2 = new Library();
           m_book2.ID = 3;//子节点3
           m_book2.ParentID = 1;//隶属父节点1
           m_book2.Subject = "C#面向对象程序设计";
           m_book2.Author = "黄聪明";
           m_book2.ISBN = "7030124847";
           m_book2.Pdate = "2004-01";
           m_book2.Version = "1";
           BookList.Add(m_book2);

           Library m_book3 = new Library();
           m_book3.ID = 4;//子节点4
           m_book3.ParentID = 1;//隶属父节点1
           m_book3.Subject = "C#程序设计案例教程";
           m_book3.Author = "王明福";
           m_book3.ISBN = "9787030239747";
           m_book3.Pdate = "2009-02";
           m_book3.Version = "1";
           BookList.Add(m_book3);

           Library m_catefory2 = new Library();
           m_catefory2.ID = 5;
           m_catefory2.Subject = "文学";
           BookList.Add(m_catefory2);

           Library m_book4 = new Library();
           m_book4.ID = 6;//子节点6
           m_book4.ParentID = 5;//隶属父节点5
           m_book4.Subject = "殷商西周散文文体研究";
           m_book4.Author = "梅軍";
           m_book4.ISBN = "9787030478566";
           m_book4.Pdate = "2016-03";
           m_book4.Version = "1";
           BookList.Add(m_book4);

           Library m_book5 = new Library();
           m_book5.ID = 7;//子节点7
           m_book5.ParentID = 5;//隶属父节点5
           m_book5.Subject = "生命无序 : 李肇星的家国情怀";
           m_book5.Author = "李肇星";
           m_book5.ISBN = "9787030285867";
           m_book5.Pdate = "2011-02";
           m_book5.Version = "1";
           BookList.Add(m_book5);


           this.treeList1.OptionsView.ShowCheckBoxes = true;// //是否显示CheckBox

           this.treeList1.OptionsBehavior.AllowIndeterminateCheckState = true;// //设置节点是否有中间状态,即一部分子节点选中,一部分子节点没有选中

           this.treeList1.DataSource = BookList;//将BookList数据绑定到treeList1上

           this.treeList1.RefreshDataSource();//刷新reeList1
       }

4.进阶功能

TreeList作为一个属性控件,必须能够实现两个基本功能:

1)TreeList带有CheckBox,节点有三种状态(全部勾选、全部不勾选、部分勾选),可通过下面代码实现;

this.treeList1.OptionsView.ShowCheckBoxes = true;// //是否显示CheckBox

this.treeList1.OptionsBehavior.AllowIndeterminateCheckState = true;// //设置节点是否有中间状态,即一部分子节点选中,一部分子节点没有选中

2)选择的父节点和子节点相互影响,例如选择了父节点则自动勾选所有的子节点,可通过下面代码实现。

/// <summary>
       /// 节点选中前事件
       /// </summary>
       /// <param name="sender"></param>
       /// <param name="e"></param>
       private void treeList1_BeforeCheckNode(object sender, DevExpress.XtraTreeList.CheckNodeEventArgs e)
       {
           e.State = (e.PrevState == CheckState.Checked ? CheckState.Unchecked : CheckState.Checked);
       }

       /// <summary>
       /// 节点选中后事件
       /// </summary>
       /// <param name="sender"></param>
       /// <param name="e"></param>
       private void treeList1_AfterCheckNode(object sender, DevExpress.XtraTreeList.NodeEventArgs e)
       {
           SetCheckedChildNodes(e.Node, e.Node.CheckState);
           SetCheckedParentNodes(e.Node, e.Node.CheckState);

       }

       /// <summary>
       /// 设置子节点的状态
       /// </summary>
       /// <param name="node"></param>
       /// <param name="check"></param>
       private void SetCheckedChildNodes(DevExpress.XtraTreeList.Nodes.TreeListNode node, CheckState check)
       {
           for (int i = 0; i < node.Nodes.Count; i++)
           {
               node.Nodes[i].CheckState = check;
               SetCheckedChildNodes(node.Nodes[i], check);
           }
       }

       /// <summary>
       /// 设置父节点的状态
       /// </summary>
       /// <param name="node"></param>
       /// <param name="check"></param>
       private void SetCheckedParentNodes(DevExpress.XtraTreeList.Nodes.TreeListNode node, CheckState check)
       {
           if (node.ParentNode != null)
           {
               bool b = false;
               CheckState state;
               for (int i = 0; i < node.ParentNode.Nodes.Count; i++)
               {
                   state = (CheckState)node.ParentNode.Nodes[i].CheckState;
                   if (!check.Equals(state))
                   {
                       b = !b;
                       break;
                   }
               }
               node.ParentNode.CheckState = b ? CheckState.Indeterminate : check;
               SetCheckedParentNodes(node.ParentNode, check);
           }
       }

代码全文:

using System;
using System.Collections;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace CreateXtraTreeList
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        /// <summary>
        /// 节点选中前事件
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void treeList1_BeforeCheckNode(object sender, DevExpress.XtraTreeList.CheckNodeEventArgs e)
        {
            e.State = (e.PrevState == CheckState.Checked ? CheckState.Unchecked : CheckState.Checked);
        }

        /// <summary>
        /// 节点选中后事件
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void treeList1_AfterCheckNode(object sender, DevExpress.XtraTreeList.NodeEventArgs e)
        {
            SetCheckedChildNodes(e.Node, e.Node.CheckState);
            SetCheckedParentNodes(e.Node, e.Node.CheckState);

        }

        /// <summary>
        /// 设置子节点的状态
        /// </summary>
        /// <param name="node"></param>
        /// <param name="check"></param>
        private void SetCheckedChildNodes(DevExpress.XtraTreeList.Nodes.TreeListNode node, CheckState check)
        {
            for (int i = 0; i < node.Nodes.Count; i++)
            {
                node.Nodes[i].CheckState = check;
                SetCheckedChildNodes(node.Nodes[i], check);
            }
        }

        /// <summary>
        /// 设置父节点的状态
        /// </summary>
        /// <param name="node"></param>
        /// <param name="check"></param>
        private void SetCheckedParentNodes(DevExpress.XtraTreeList.Nodes.TreeListNode node, CheckState check)
        {
            if (node.ParentNode != null)
            {
                bool b = false;
                CheckState state;
                for (int i = 0; i < node.ParentNode.Nodes.Count; i++)
                {
                    state = (CheckState)node.ParentNode.Nodes[i].CheckState;
                    if (!check.Equals(state))
                    {
                        b = !b;
                        break;
                    }
                }
                node.ParentNode.CheckState = b ? CheckState.Indeterminate : check;
                SetCheckedParentNodes(node.ParentNode, check);
            }
        }


        private void Form1_Load(object sender, EventArgs e)
        {
            ArrayList BookList = new ArrayList();//声明一个ArrayList存放图书信息

            Library m_catefory1 = new Library();
            m_catefory1.ID = 1;
            m_catefory1.Subject = "计算机科学与技术";          
            BookList.Add(m_catefory1);

            Library m_book1 = new Library();
            m_book1.ID = 2;//子节点2
            m_book1.ParentID = 1;//隶属父节点1
            m_book1.Subject = "C#设计模式";
            m_book1.Author = "(美)James W. Cooper";
            m_book1.ISBN = "9787030308153";
            m_book1.Pdate = "2011-07";
            m_book1.Version = "1";
            BookList.Add(m_book1);

            Library m_book2 = new Library();
            m_book2.ID = 3;//子节点3
            m_book2.ParentID = 1;//隶属父节点1
            m_book2.Subject = "C#面向对象程序设计";
            m_book2.Author = "黄聪明";
            m_book2.ISBN = "7030124847";
            m_book2.Pdate = "2004-01";
            m_book2.Version = "1";
            BookList.Add(m_book2);

            Library m_book3 = new Library();
            m_book3.ID = 4;//子节点4
            m_book3.ParentID = 1;//隶属父节点1
            m_book3.Subject = "C#程序设计案例教程";
            m_book3.Author = "王明福";
            m_book3.ISBN = "9787030239747";
            m_book3.Pdate = "2009-02";
            m_book3.Version = "1";
            BookList.Add(m_book3);

            Library m_catefory2 = new Library();
            m_catefory2.ID = 5;
            m_catefory2.Subject = "文学";
            BookList.Add(m_catefory2);

            Library m_book4 = new Library();
            m_book4.ID = 6;//子节点6
            m_book4.ParentID = 5;//隶属父节点5
            m_book4.Subject = "殷商西周散文文体研究";
            m_book4.Author = "梅軍";
            m_book4.ISBN = "9787030478566";
            m_book4.Pdate = "2016-03";
            m_book4.Version = "1";
            BookList.Add(m_book4);

            Library m_book5 = new Library();
            m_book5.ID = 7;//子节点7
            m_book5.ParentID = 5;//隶属父节点5
            m_book5.Subject = "生命无序 : 李肇星的家国情怀";
            m_book5.Author = "李肇星";
            m_book5.ISBN = "9787030285867";
            m_book5.Pdate = "2011-02";
            m_book5.Version = "1";
            BookList.Add(m_book5);


            this.treeList1.OptionsView.ShowCheckBoxes = true;// //是否显示CheckBox

            this.treeList1.OptionsBehavior.AllowIndeterminateCheckState = true;// //设置节点是否有中间状态,即一部分子节点选中,一部分子节点没有选中

            this.treeList1.DataSource = BookList;//将BookList数据绑定到treeList1上

            this.treeList1.RefreshDataSource();//刷新reeList1
        }

        private void Button1_Click(object sender, EventArgs e)
        {
            System.Diagnostics.Process.Start("https://www.daboke.com");//欢迎访问大博客,阅读更多编程实战案例!
        }

        private void Button2_Click(object sender, EventArgs e)
        {
            System.Diagnostics.Process.Start("https://www.daboke.com/devexpress/xtratreelist.html");//原文链接!
        }

        private void Button3_Click(object sender, EventArgs e)
        {
            System.Diagnostics.Process.Start("https://space.bilibili.com/580719958");//欢迎访问我的B站频道-编程自修室,观看更多C#编程实战视频!
        }
    }
}

原文链接:https://www.daboke.com/devexpress/xtratreelist.html

B站up主-编程自修室:https://space.bilibili.com/580719958

源码下载:CreateXtraTreeList

You may also like...

3 Responses

  1. 刷百度下拉说道:

    很不错的测试功能了

发表评论

电子邮件地址不会被公开。

微信 OR 支付宝 扫描二维码
为本文作者 打个赏
pay_weixin pay_weixin
最喜欢你一言不合就打赏的样子了^_^