DEV控件使用之TreeList

来源:互联网 发布:json数据里的函数 编辑:程序博客网 时间:2024/05/16 01:49

1. 拖一个TreeList控件到form中;

2.创建一个静态类,用来生成datasource

using System;using System.Collections.Generic;namespace TreeList_DataBinding {    public class Employee {        public int ID { get; set; }        public int ParentID { get; set; }        public string Name { get; set; }        public string Position { get; set; }        public string Department { get; set; }    }    public static class Stuff {        public static List<Employee> GetStuff() {            List<Employee> stuff = new List<Employee>();            stuff.Add(new Employee() { ID = 1, ParentID = 0, Name = "Gregory S. Price", Department = "", Position = "President" });            stuff.Add(new Employee() { ID = 2, ParentID = 1, Name = "Irma R. Marshall", Department = "Marketing", Position = "Vice President" });            stuff.Add(new Employee() { ID = 3, ParentID = 1, Name = "John C. Powell", Department = "Operations", Position = "Vice President" });            stuff.Add(new Employee() { ID = 4, ParentID = 1, Name = "Christian P. Laclair", Department = "Production", Position = "Vice President" });            stuff.Add(new Employee() { ID = 5, ParentID = 1, Name = "Karen J. Kelly", Department = "Finance", Position = "Vice President" });            stuff.Add(new Employee() { ID = 6, ParentID = 2, Name = "Brian C. Cowling", Department = "Marketing", Position = "Manager" });            stuff.Add(new Employee() { ID = 7, ParentID = 2, Name = "Thomas C. Dawson", Department = "Marketing", Position = "Manager" });            stuff.Add(new Employee() { ID = 8, ParentID = 2, Name = "Angel M. Wilson", Department = "Marketing", Position = "Manager" });            stuff.Add(new Employee() { ID = 9, ParentID = 2, Name = "Bryan R. Henderson", Department = "Marketing", Position = "Manager" });            stuff.Add(new Employee() { ID = 10, ParentID = 3, Name = "Harold S. Brandes", Department = "Operations", Position = "Manager" });            stuff.Add(new Employee() { ID = 11, ParentID = 3, Name = "Michael S. Blevins", Department = "Operations", Position = "Manager" });            stuff.Add(new Employee() { ID = 12, ParentID = 3, Name = "Jan K. Sisk", Department = "Operations", Position = "Manager" });            stuff.Add(new Employee() { ID = 13, ParentID = 3, Name = "Sidney L. Holder", Department = "Operations", Position = "Manager" });            stuff.Add(new Employee() { ID = 14, ParentID = 4, Name = "James L. Kelsey", Department = "Production", Position = "Manager" });            stuff.Add(new Employee() { ID = 15, ParentID = 4, Name = "Howard M. Carpenter", Department = "Production", Position = "Manager" });            stuff.Add(new Employee() { ID = 16, ParentID = 4, Name = "Jennifer T. Tapia", Department = "Production", Position = "Manager" });            stuff.Add(new Employee() { ID = 17, ParentID = 5, Name = "Judith P. Underhill", Department = "Finance", Position = "Manager" });            stuff.Add(new Employee() { ID = 18, ParentID = 5, Name = "Russell E. Belton", Department = "Finance", Position = "Manager" });            return stuff;        }    }}


3. 在Form.cs中添加

        public MainForm()        {            InitializeComponent();             InitTreeList();        } 
void InitTreeList()        {            treeList1.DataSource = Station.GetStation();            treeList1.RefreshDataSource();                   }

此时运行程序已可以看到TreeList生成的树型结构,但还有一些个性化的设置可以属性中实现。如下:

1)隐藏列头

optionview -->showcolums =false;

2)为不同级别的节点添加不同的图标

a) 添加一个ImageCollection控件,并在控件的ImageSize属性中设置图标的大小,然后添加图标。

注意:如果先添加再图标再设置ImageSize属性,则会清空ImageCollection. 所以最好先设置大小再添加图标。

b) 设置TreeList的SelectImageList为刚才所添加的ImageCollection

c) 在TreeList的 GetSelectImage方法中添加代码,判断节点的级别,并选择对应的ImageIndex. 添加代码如下:

 private void treeList1_GetSelectImage(object sender, DevExpress.XtraTreeList.GetSelectImageEventArgs e)        {            if (!(e.Node.ParentNode == null))            {                if (!e.Node.HasChildren)                {                    e.NodeImageIndex = 2;                }                else                {                    e.NodeImageIndex = 1;                }            }        }


以上部分代码来自DevExpress Documentation 13.2

0 0