学海无涯

导航

Dev TreeList 树形结构

一.您将treeList.OptionsView.ShowCheckBoxes设置为True,树形结构前就会出现CheckBox选择框,如果您想达到选择父节点,子节点也同时选中的效果,需将treeList.OptionsBehavior.AllowRecursiveNodeChecking设置为True。

 

 设置完即可看到效果,如图:

 二.扩展方法

namespace LG.APS.Client.CustomControls.Extensions
{
    public static class TreeListExtension
    {
        private static List<TreeListNode> selectedTreeListNodes = new List<TreeListNode>();
        /// <summary>
        /// 得到已选择的全部节点
        /// </summary>
        /// <param name="parentNode"></param>
        /// <returns></returns>
        private static List<TreeListNode> GetAllCheckedNodes(TreeListNode parentNode)
        {
            if (parentNode.Nodes.Count == 0)
            {
                return selectedTreeListNodes;//递归终止
            }
            foreach (TreeListNode node in parentNode.Nodes)
            {
                if (node.CheckState == CheckState.Checked)//判断当前节点选择状态
                {
                    selectedTreeListNodes.Add(node);
                }
                if (node.HasChildren)
                {
                    GetAllCheckedNodes(node);//执行递归
                }
            }
            return selectedTreeListNodes;
        }
        /// <summary>
        /// 得到已选择的全部节点
        /// </summary>
        /// <param name="treeList"></param>
        /// <returns></returns>
        public static List<TreeListNode> GetAllCheckedNodes(this TreeList treeList)
        {
            selectedTreeListNodes.Clear();
            List<TreeListNode> selectedNodes = new List<TreeListNode>();
            if (treeList.Nodes.Count > 0)
            {
                foreach (TreeListNode node in treeList.Nodes)//拿所有结点
                {
                    selectedNodes.AddRange(GetAllCheckedNodes(node));
                }
            }
            return selectedNodes;
        }
        /// <summary>
        /// 得到已选择的他部节点对应的对象实例列表
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="treeList"></param>
        /// <param name="parentNode"></param>
        /// <returns></returns>
        public static List<T> GetAllCheckedObject<T>(this TreeList treeList)
        {
            List<T> results = new List<T>();
            List<TreeListNode> selectedTreeListNodes = new List<TreeListNode>();
            if (treeList.Nodes.Count > 0)
            {
                selectedTreeListNodes = treeList.GetAllCheckedNodes();
            }
            foreach (var node in selectedTreeListNodes)
            {
                results.Add((T)treeList.GetDataRecordByNode(node));
            }
            return results;
        }
    }
}

  

 

官方例子:

https://docs.devexpress.com/WindowsForms/120672/controls-and-libraries/tree-list/feature-center/focus-selection-and-navigation/node-checking-checkboxes-and-radio-buttons

https://www.cnblogs.com/improveself/articles/12195941.html

posted on 2024-03-14 16:13  宁静致远.  阅读(67)  评论(0编辑  收藏  举报