关于DevExpress的XtraTreeList使用方法总结

在最近的工作中,使用到了DevExpress中的XtraTreeList用法,正好借此机会研究一下此控件的应用,现将其应用技巧与大家分享一下。

1、使用XtraTreeList绑定数据(数据格式可以为ArrayList形式)

我们实例说明要实现下面样式的窗体:

首先,在Run Designer里面添加两个列(columns),设置两列的属性,其中第一列“名称”为string格式,FieldName为Name;第二列“选择”将ColumnEdit属性设置为CheckEdit1,FieldName为IsChecked,设置完毕。

其次可以编写一个类,用来绑定TreeList,针对此TreeList窗体,其类代码设置如下:

public class TestTreeList

    {

        public TestTreeList()

        {

        }

        //名称字段变量

        private string m_sName = string.Empty;

        //选择字段变量

        private bool m_bIsChecked = false;

        //子Node节点ID变量

        private int m_iID = -1;

        //父Node节点ID变量

        private int m_iParentID = -1;

 

        public int ID

        {

            get

            {

                return m_iID;

            }

            set

            {

                m_iID = value;

            }

        }

        public int ParentID

        {

            get

            {

                return m_iParentID;

            }

            set

            {

                m_iParentID = value;

            }

        }

        public string Name

        {

            get

            {

                return m_sName;

            }

            set

            {

                m_sName = value;

            }

        }

 

        public bool IsChecked

        {

            get

            {

                return m_bIsChecked;

            }

            set

            {

                m_bIsChecked = value;

            }

        }

}

类构造完成,在FormLoad里面借助此类对TreeList进行绑定:

  private void Form1_Load(object sender, EventArgs e)

        {

            ArrayList pList = new ArrayList();

            TestTreeList p = new TestTreeList();

            p.Name = "测试1";

            p.IsChecked = true;

            p.ID = 1;

            pList.Add(p);

            TestTreeList q = new TestTreeList();

            q.Name = "测试2";

            q.IsChecked = false;

            q.ParentID = 1;

            q.ID = 2;

            pList.Add(q);

            this.treeList1.DataSource = pList;

            this.treeList1.RefreshDataSource();

 

        }

这样就可以构造出一个简单的TreeList应用方法,程序运行结果可得第一幅图所示。

2、DevTreeList改变样式:

如果将TreeList控件改变成如下样式(同第一幅图不一样,将Check控件显示在节点最前面)。

 

可以直接修改其控件的ShowCheckBoxes属性即可(其它样式类似如下)。

 

3、DevTreeList非绑定形式实现:动态加载,构建:

运行样式如下:

 

实现动态加载的几个函数实现如下:

/// <summary>

        /// 动态创建TreeList的Column,一列为图层名,一列为是否统计列Check控件

        /// </summary>

        private void creatTreeListControl()

        {

            DevExpress.XtraTreeList.Columns.TreeListColumn treeListColumnLayerName= new DevExpress.XtraTreeList.Columns.TreeListColumn();

            treeListColumnLayerName.Caption = "名称";

            treeListColumnLayerName.FieldName = "LayerName";

            treeListColumnLayerName.MinWidth = 38;

            treeListColumnLayerName.Name = "treeListColumnLayerName";

            treeListColumnLayerName.Visible = true;

            treeListColumnLayerName.VisibleIndex = 0;

            treeListColumnLayerName.Width = 111;

 

            DevExpress.XtraTreeList.Columns.TreeListColumn treeListColumnStatisticValue=new DevExpress.XtraTreeList.Columns.TreeListColumn();

              treeListColumnStatisticValue.Caption = "统计值";

            treeListColumnStatisticValue.ColumnEdit = this.repositoryItemCheckEdit1;

            treeListColumnStatisticValue.FieldName = "StatisticValue";

            treeListColumnStatisticValue.Name = "treeListColumnStatisticValue";

            treeListColumnStatisticValue.Visible = true;

            treeListColumnStatisticValue.VisibleIndex = 1;

 

            this.treeListLayerControl.Columns.AddRange(new DevExpress.XtraTreeList.Columns.TreeListColumn[] {

            treeListColumnLayerName,

            treeListColumnStatisticValue});

            this.treeListLayerControl.Nodes.Clear();

            this.treeListLayerControl.Refresh();

 

        }

/// <summary>

        /// 动态创建TreeList Layer Node节点

        /// </summary>

        /// <param name="layerName"></param>

        /// <param name="check"></param>

        private void creatMainLayerNode(string layerName,bool check)

        {

            this.treeListLayerControl.BeginUnboundLoad();

            this.treeListLayerControl.AppendNode(new object[] {  layerName,check}, -1);

            this.treeListLayerControl.EndUnboundLoad();

        }

    /// <summary>

        /// 动态创建TreeList 字段属性值 Node节点

        /// </summary>

        /// <param name="ValueName"></param>

        /// <param name="check"></param>

        /// <param name="ID"></param>

        private void CreatChildNode(string ValueName, bool check,int ID)

        {

            this.treeListLayerControl.BeginUnboundLoad();

            this.treeListLayerControl.AppendNode(new object[] { ValueName, check }, ID);

            this.treeListLayerControl.EndUnboundLoad();

        }

  /// <summary>

        /// 获得图层名节点的Index值

        /// </summary>

        /// <param name="ParentNodeName"></param>

        /// <returns></returns>

        private int getParentID(string ParentNodeName)

        {

            int i = -1;

            for (i = 0; i < this.treeListLayerControl.Nodes.Count;i++ )

            {

                if (this.treeListLayerControl.Nodes[i][0].ToString() == ParentNodeName)

                {                 

                    break;

                }

 

            }

            return i;

 

        }

    /// <summary>

        /// 当点击Node事件发生改变(点击主节点时,其子节点跟着主节点变化)

        /// </summary>

        /// <param name="sender"></param>

        /// <param name="e"></param>

        private void treeListLayerControl_CellValueChanging(object sender, DevExpress.XtraTreeList.CellValueChangedEventArgs e)

        {

            if (e.Column.Name.Equals("treeListColumnStatisticValue") && e.Node.HasChildren)

            {

                object objCol = this.treeListLayerControl.Columns[1];

                for (int i = 0; i < e.Node.Nodes.Count; i++)

                {

                    e.Node.Nodes[i].SetValue(objCol, e.Value);

                }

                e.Node.SetValue(objCol, e.Value);

            }

        }

创建子节点方法:

      int indexLayerTree = this.getParentID(playerInfo.LayerName);

                    if (indexLayerTree == -1)

                        return;

                    CreatChildNode(sValue, false, this.treeListLayerControl.Nodes[indexLayerTree].Id);

其创建主节点和创建子节点方法类似,比创建子节点简单的多。

4、总结

以上为个人研究DevExpress TreeList使用的大致方法,尽管文笔简单潦草,但是针对TreeList的基本使用足矣。

另外,DevExpress控件可以使系统界面做的十分漂亮,我的截图界面显得丑陋,只因为是实验程序,并不是真正应用,还请大家不要误解。

posted @ 2011-03-10 20:55  @龙飞凤舞@  阅读(41488)  评论(11编辑  收藏  举报