treelist控件7.2以后版本

树形控件是使用频率很高的一种控件。对于属性控件往往需要下面两个功能

1.TreeList带有CheckBox,并且节点要有三种状态(所有的子节点都选中,所有的子节点都没选择,一部分子节点选中)。使用DevXpress的TreeList控件很容易实现这一功能。

设置TreeList.OptionsView.ShowCheckBoxes = true            //是否显示CheckBox

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

设置这两个属性之后就实现了TreeList带有CheckBox,并且节点有三种状态。

 

2.选中父节点或者子节点相互影响的功能,如选择父节点选择所有子节点。绑定TreeList的两个事件AfterCheckNode和BeforeCheckNode

实现功能的代码如下:

 

        private void treeList1_AfterCheckNode(object sender, DevExpress.XtraTreeList.NodeEventArgs e)
        {
            SetCheckedChildNodes(e.Node, e.Node.CheckState);
            SetCheckedParentNodes(e.Node, e.Node.CheckState);

        }

        private void treeList1_BeforeCheckNode(object sender, DevExpress.XtraTreeList.CheckNodeEventArgs e)
        {
            e.State = (e.PrevState == CheckState.Checked ? CheckState.Unchecked : CheckState.Checked);
        }

        /// <summary>
        /// 设置子节点的状态
        /// </summary>
        /// <param name="node"></param>
        /// <param name="check"></param>
        private void SetCheckedChildNodes(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(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);
            }
        }

============================================================================

1.TreeList.NodeCellStyle事件

Node的显示(包括窗口的切换导致Node的显示)和状态的改变都会触发该事件。该事件主要用来改变Node的显示样式。

 private void treeList1_NodeCellStyle(object sender, DevExpress.XtraTreeList.GetCustomNodeCellStyleEventArgs e)
        {
            if (e.Node.CheckState == CheckState.Unchecked)
            {
                e.Appearance.Font = new Font(DevExpress.Utils.AppearanceObject.DefaultFont, FontStyle.Strikeout);
                e.Appearance.ForeColor = Color.Gray;
            }
        }

上面的代码是实现的效果是 : CheckState为Unchecked的节点的字带有中划线且背景灰色。

 

2.TreeList.DoubleClick事件

双击Node时触发,但要注意的是要在TreeList.OptionsBehavior.Editable = false的情况下,双击Node才能触发该事件。

        private void treeList1_DoubleClick(object sender, EventArgs e)
        {
            TreeListNode clickedNode = this.treeList1.FocusedNode;
            string disPlayText = clickedNode.GetDisplayText("test"); 
            MessageBox.Show("You clicked " + disPlayText);
        }

 

3.TreeList的命中测试特性

 private void treeList1_MouseMove(object sender, MouseEventArgs e)
        {
            Point point = treeList1.PointToClient(Cursor.Position);
            TreeListHitInfo hitInfo = treeList1.CalcHitInfo(point);
            switch (hitInfo.HitInfoType)
            {
                case HitInfoType.Cell:
                    this.Cursor = Cursors.Hand;
                    break;
                case HitInfoType.NodeCheckBox:
                    this.Cursor = Cursors.PanEast;
                    break;
                default :
                    this.Cursor = Cursors.Default;
                    break;
            }
        }

 

 

 

 

 

 

posted @ 2013-04-11 08:53  咸鱼公子  Views(506)  Comments(0Edit  收藏  举报