DevExpress中TreeList控件的使用

DevExpress中树形结构的显示需要用到TreeList控件,但是区别于其他的第三方控件,TreeList的数据源绑定方式有些特别,它主要利用子节点编号(Id)和父节点编号(ParentId)确定各个树节点的从属关系,示例代码如下:

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

namespace DXApplication1
{
    public partial class Form1 : DevExpress.XtraEditors.XtraForm
    {
        public Form1()
        {
            InitializeComponent();
            InitializeTreeComponent();
        }

        // 初始化树组件
        private void InitializeTreeComponent()
        {
            List<Car> list = new List<Car>();

            // 宝马系列
            list.Add(new Car() { Id = 1, ParentId = 1, Name = "宝马系列" });
            list.Add(new Car() { Id = 2, ParentId = 1, Name = "宝马3系" });
            list.Add(new Car() { Id = 3, ParentId = 1, Name = "宝马5系" });
            list.Add(new Car() { Id = 4, ParentId = 1, Name = "宝马7系" });

            // 奥迪系列
            list.Add(new Car() { Id = 5, ParentId = 5, Name = "奥迪系列" });
            list.Add(new Car() { Id = 6, ParentId = 5, Name = "奥迪A4" });
            list.Add(new Car() { Id = 7, ParentId = 5, Name = "奥迪A6" });
            list.Add(new Car() { Id = 8, ParentId = 5, Name = "奥迪A8" });

            // 大众系列
            list.Add(new Car() { Id = 9, ParentId = 9, Name = "大众系列" });
            list.Add(new Car() { Id = 10, ParentId = 9, Name = "大众帕萨特" });
            list.Add(new Car() { Id = 11, ParentId = 9, Name = "大众途昂" });
            list.Add(new Car() { Id = 12, ParentId = 9, Name = "大众辉昂" });

            // 绑定TreeList
            treeList1.DataSource = list;
            treeList1.KeyFieldName = "Id";
            treeList1.ParentFieldName = "ParentId";
            treeList1.Columns[0].Caption = "轿车";
            treeList1.OptionsBehavior.Editable = false;
            treeList1.RowHeight = 20;
            treeList1.ExpandAll();
        }
    }

    public class Car
    {
        /// <summary>
        /// 节点编号
        /// </summary>
        public int Id { get; set; }

        /// <summary>
        /// 父节点编号
        /// </summary>
        public int ParentId { get; set; }

        /// <summary>
        /// 车名
        /// </summary>
        public string Name { get; set; }
    }
}

程序运行结果如下图所示:
在这里插入图片描述
如果需要给节点添加图片,则需要绑定ImageList,代码如下:

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

namespace DXApplication1
{
    public partial class Form1 : DevExpress.XtraEditors.XtraForm
    {
        public Form1()
        {
            InitializeComponent();
            InitializeTreeComponent();
        }

        // 初始化树组件
        private void InitializeTreeComponent()
        {
            List<Car> list = new List<Car>();

            // 宝马系列
            list.Add(new Car() { Id = 1, ParentId = 1, Name = "宝马系列" });
            list.Add(new Car() { Id = 2, ParentId = 1, Name = "宝马3系" });
            list.Add(new Car() { Id = 3, ParentId = 1, Name = "宝马5系" });
            list.Add(new Car() { Id = 4, ParentId = 1, Name = "宝马7系" });

            // 奥迪系列
            list.Add(new Car() { Id = 5, ParentId = 5, Name = "奥迪系列" });
            list.Add(new Car() { Id = 6, ParentId = 5, Name = "奥迪A4" });
            list.Add(new Car() { Id = 7, ParentId = 5, Name = "奥迪A6" });
            list.Add(new Car() { Id = 8, ParentId = 5, Name = "奥迪A8" });

            // 大众系列
            list.Add(new Car() { Id = 9, ParentId = 9, Name = "大众系列" });
            list.Add(new Car() { Id = 10, ParentId = 9, Name = "大众帕萨特" });
            list.Add(new Car() { Id = 11, ParentId = 9, Name = "大众途昂" });
            list.Add(new Car() { Id = 12, ParentId = 9, Name = "大众辉昂" });

            // 图片列表
            ImageList imageList = new ImageList();
            imageList.Images.Add(Bitmap.FromFile(Application.StartupPath + "\\icon\\a.png"));
            imageList.Images.Add(Bitmap.FromFile(Application.StartupPath + "\\icon\\b.png"));

            // 绑定TreeList
            treeList1.DataSource = list;
            treeList1.KeyFieldName = "Id";
            treeList1.ParentFieldName = "ParentId";
            treeList1.Columns[0].Caption = "轿车";
            treeList1.OptionsBehavior.Editable = false;
            treeList1.RowHeight = 20;
            treeList1.SelectImageList = imageList;

            // 设置图标
            foreach (TreeListNode root in treeList1.Nodes)
            {
                root.ImageIndex = 0;
                root.SelectImageIndex = 0;
                foreach (TreeListNode child in root.Nodes)
                {
                    child.ImageIndex = 1;
                    child.SelectImageIndex = 1;
                }
            }

            // 展开节点
            treeList1.ExpandAll();
        }
    }

    public class Car
    {
        /// <summary>
        /// 节点编号
        /// </summary>
        public int Id { get; set; }

        /// <summary>
        /// 父节点编号
        /// </summary>
        public int ParentId { get; set; }

        /// <summary>
        /// 车名
        /// </summary>
        public string Name { get; set; }
    }
}

程序运行结果如下所示:
在这里插入图片描述
在使用树控件的时候,必不可少的就是点击树节点并获取节点值,但TreeList并没有提供类似NodeClick这样的事件,如果想要获取TreeList的节点值,需要使用FocusedNodeChanged事件,代码如下:

using DevExpress.XtraEditors;
using DevExpress.XtraTreeList.Nodes;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace DXApplication1
{
    public partial class Form1 : DevExpress.XtraEditors.XtraForm
    {
        public Form1()
        {
            InitializeComponent();
            InitializeTreeComponent();
        }

        // 初始化树组件
        private void InitializeTreeComponent()
        {
            List<Car> list = new List<Car>();

            // 宝马系列
            list.Add(new Car() { Id = 1, ParentId = 1, Name = "宝马系列" });
            list.Add(new Car() { Id = 2, ParentId = 1, Name = "宝马3系" });
            list.Add(new Car() { Id = 3, ParentId = 1, Name = "宝马5系" });
            list.Add(new Car() { Id = 4, ParentId = 1, Name = "宝马7系" });

            // 奥迪系列
            list.Add(new Car() { Id = 5, ParentId = 5, Name = "奥迪系列" });
            list.Add(new Car() { Id = 6, ParentId = 5, Name = "奥迪A4" });
            list.Add(new Car() { Id = 7, ParentId = 5, Name = "奥迪A6" });
            list.Add(new Car() { Id = 8, ParentId = 5, Name = "奥迪A8" });

            // 大众系列
            list.Add(new Car() { Id = 9, ParentId = 9, Name = "大众系列" });
            list.Add(new Car() { Id = 10, ParentId = 9, Name = "大众帕萨特" });
            list.Add(new Car() { Id = 11, ParentId = 9, Name = "大众途昂" });
            list.Add(new Car() { Id = 12, ParentId = 9, Name = "大众辉昂" });

            // 图片列表
            ImageList imageList = new ImageList();
            imageList.Images.Add(Bitmap.FromFile(Application.StartupPath + "\\icon\\a.png"));
            imageList.Images.Add(Bitmap.FromFile(Application.StartupPath + "\\icon\\b.png"));

            // 绑定TreeList
            treeList1.DataSource = list;
            treeList1.KeyFieldName = "Id";
            treeList1.ParentFieldName = "ParentId";
            treeList1.Columns[0].Caption = "轿车";
            treeList1.OptionsBehavior.Editable = false;
            treeList1.RowHeight = 20;
            treeList1.SelectImageList = imageList;

            // 设置图标
            foreach (TreeListNode root in treeList1.Nodes)
            {
                root.ImageIndex = 0;
                root.SelectImageIndex = 0;
                foreach (TreeListNode child in root.Nodes)
                {
                    child.ImageIndex = 1;
                    child.SelectImageIndex = 1;
                }
            }

            // 展开节点
            treeList1.ExpandAll();
        }

        private void treeList1_FocusedNodeChanged(object sender, DevExpress.XtraTreeList.FocusedNodeChangedEventArgs e)
        {
            string nodeText = e.Node.GetDisplayText(0).ToString();
            if (e.Node.ParentNode == null && e.Node.HasChildren)
            {
                XtraMessageBox.Show("该节点为父节点,节点值为:" + nodeText, "提示", MessageBoxButtons.OK, MessageBoxIcon.Asterisk);
            }
            if (e.Node.ParentNode != null && !e.Node.HasChildren)
            {
                XtraMessageBox.Show("该节点为子节点,节点值为:" + nodeText, "提示", MessageBoxButtons.OK, MessageBoxIcon.Asterisk);
            }
        }
    }

    public class Car
    {
        /// <summary>
        /// 节点编号
        /// </summary>
        public int Id { get; set; }

        /// <summary>
        /// 父节点编号
        /// </summary>
        public int ParentId { get; set; }

        /// <summary>
        /// 车名
        /// </summary>
        public string Name { get; set; }
    }
}

程序运行结果如下所示:
在这里插入图片描述

发布了99 篇原创文章 · 获赞 16 · 访问量 3万+

猜你喜欢

转载自blog.csdn.net/HerryDong/article/details/103672451
今日推荐