zoukankan      html  css  js  c++  java
  • Devexpress TreeList选择父级联动

    Treelist当显示复选框后,父级和子级的复选框没有关联,使用过程中很不便,如图所示

    image

    自己给treelist添加父子级联动

    /// <summary>
        /// 初始化TreeList,父子节点选择关联
        /// </summary>
        public class TreeListInitial
        {
            TreeList Tree;
            /// <summary>
            /// 初始化TreeList,父子节点选择关联
            /// </summary>
            /// <param name="tree"></param>
            public TreeListInitial(TreeList tree)
            {
                Tree = tree;
                tree.BeforeCheckNode += tree_BeforeCheckNode;
                tree.AfterCheckNode += tree_AfterCheckNode;
            }
    
            public bool AllowCheck = true;
    
            public void tree_AfterCheckNode(object sender, DevExpress.XtraTreeList.NodeEventArgs e)
            {
                SetCheckedChildNodes(e.Node, e.Node.CheckState);
                SetCheckedParentNodes(e.Node, e.Node.CheckState);
            }
    
            void tree_BeforeCheckNode(object sender, DevExpress.XtraTreeList.CheckNodeEventArgs e)
            {
                e.CanCheck = AllowCheck;
    
    
                e.State = (e.PrevState == CheckState.Checked ? CheckState.Unchecked : CheckState.Checked);
            }
    
            public void SetNodeCheckState(TreeListNode node, CheckState checkState)
            {
                Tree.SetNodeCheckState(node, CheckState.Checked);
                SetCheckedChildNodes(node, CheckState.Checked);
                SetCheckedParentNodes(node, CheckState.Checked);
            }
    
            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);
    
                }
            }
            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);
                }
            }
    
    
        }

    在窗体加载的时候传入需要联动的treelist实例化即可

    new TreeListInitial(tree_Module)

    效果图:

    image

  • 相关阅读:
    668. Kth Smallest Number in Multiplication Table
    658. Find K Closest Elements
    483. Smallest Good Base
    475. Heaters
    454. 4Sum II
    441. Arranging Coins
    436. Find Right Interval
    410. Split Array Largest Sum
    392. Is Subsequence
    378. Kth Smallest Element in a Sorted Matrix
  • 原文地址:https://www.cnblogs.com/DoNetCShap/p/8889241.html
Copyright © 2011-2022 走看看