开发者论坛

 找回密码
 注册 (请使用非IE浏览器)
查看: 3391|回复: 1

DevTreeList 扩展方法简单例子(续)

[复制链接]

0

精华

95

贡献

34

赞扬

帖子
101
软币
823
在线时间
98 小时
注册时间
2013-8-21
发表于 2014-9-26 00:13:55 | 显示全部楼层 |阅读模式
[C#] 纯文本查看 复制代码
 public delegate string BuildPathRule(string nodeText, string fullPathInfo);
        /// <summary>
        /// 获取选中节点到根节点的所有信息
        /// </summary>
        /// <param name="focusedNode">TreeListNode</param>
        /// <param name="columnID">列名称</param>
        /// <param name="buildPathRule">规则委托</param>
        /// <returns>路径信息</returns>
        public static string FullPathInfo(this TreeListNode focusedNode, string columnID, BuildPathRule buildPathRule)
        {
            if (focusedNode == null)
                throw new ArgumentNullException("focusedNode");
            if (string.IsNullOrEmpty("columnID"))
                throw new ArgumentNullException("columnID");
            string _fullPathInfo = string.Empty;
            _fullPathInfo = focusedNode.GetDisplayText(columnID);
            while (focusedNode.ParentNode != null)
            {
                focusedNode = focusedNode.ParentNode;
                string _nodeText = focusedNode.GetDisplayText(columnID).Trim();
                _fullPathInfo = buildPathRule(_nodeText, _fullPathInfo);
            }
            return _fullPathInfo;
        }
        public delegate bool CompareNodeRule(TreeListNode focusedNode);
        /// <summary>
        /// 获取筛选节点到根节点的所有信息
        /// </summary>
        /// <param name="focusedNode">TreeListNode</param>
        /// <param name="columnID">列名称</param>
        /// <param name="compareNodeRule">规则委托</param>
        /// <param name="buildPathRule">规则委托</param>
        /// <returns>路径信息</returns>
        public static string FilterPathInfo(this TreeListNode focusedNode, string columnID, CompareNodeRule compareNodeRule, BuildPathRule buildPathRule)
        {
            if (focusedNode == null)
                throw new ArgumentNullException("focusedNode");
            if (string.IsNullOrEmpty("columnID"))
                throw new ArgumentNullException("columnID");
            string _fullPathInfo = string.Empty;
            _fullPathInfo = focusedNode.GetDisplayText(columnID);
            while (focusedNode.ParentNode != null)
            {
                focusedNode = focusedNode.ParentNode;
                if (compareNodeRule(focusedNode))
                {
                    string _nodeText = focusedNode.GetDisplayText(columnID).Trim();
                    _fullPathInfo = buildPathRule(_nodeText, _fullPathInfo);
                }
            }
            return _fullPathInfo;
        }
        /// <summary>
        /// 递归遍历树节点
        /// </summary>
        /// <param name="tree"></param>
        /// <param name="opreateRule"></param>
        public static void LoopTree(this TreeList tree, Action<TreeListNode> opreateRule)
        {
            if (tree == null)
                throw new ArgumentNullException("tree");
            foreach (TreeListNode node in tree.Nodes)
            {
                opreateRule(node);
                if (node.Nodes.Count > 0)
                {
                    LoopTreeNodes(node, opreateRule);
                }
            }
        }
        /// <summary>
        /// 递归遍历TreeListNode节点
        /// </summary>
        /// <param name="node"></param>
        /// <param name="opreateRule"></param>
        public static void LoopTreeNodes(this TreeListNode node, Action<TreeListNode> opreateRule)
        {
            if (node == null)
                throw new ArgumentNullException("node");
            foreach (TreeListNode _childNode in node.Nodes)
            {
                opreateRule(_childNode);
                LoopTreeNodes(_childNode, opreateRule);
            }
        }
        /// <summary>
        /// 递归遍历TreeListNode,当opreateRule返回false停止循环
        /// </summary>
        /// <param name="node">TreeListNode</param>
        /// <param name="opreateRule">Func<TreeListNode, bool></param>
        public static void LoopTreeNodes_Break(this TreeListNode node, Func<TreeListNode, bool> opreateRule)
        {
            if (node == null)
                throw new ArgumentNullException("node");
            foreach (TreeListNode _childNode in node.Nodes)
            {
                if (!opreateRule(_childNode))
                    break;
                LoopTreeNodes_Break(_childNode, opreateRule);
            }
        }
        /// <summary>
        /// 递归遍历TreeListNode,当opreateRule返回false跳出循环,直接进入下次循环
        /// </summary>
        /// <param name="node">TreeListNode</param>
        /// <param name="opreateRule">Func<TreeListNode, bool></param>
        public static void LoopTreeNodes_Continue(this TreeListNode node, Func<TreeListNode, bool> opreateRule)
        {
            if (node == null)
                throw new ArgumentNullException("node");
            foreach (TreeListNode _childNode in node.Nodes)
            {
                if (!opreateRule(_childNode))
                    continue;
                LoopTreeNodes_Continue(_childNode, opreateRule);
            }
        }

        /// <summary>
        /// 水平滚动条
        /// </summary>
        /// <param name="tree">TreeList</param>
        public static void HorzScroll(this TreeList tree)
        {
            if (tree == null)
                throw new ArgumentNullException("tree");
            tree.OptionsView.AutoWidth = false;
            tree.BestFitColumns();
            tree.HorzScrollVisibility = ScrollVisibility.Always;
        }
        /// <summary>
        /// 为TreeList附加右键菜单
        /// MouseUp(object sender, MouseEventArgs e)事件中调用
        /// </summary>
        /// <param name="tree">TreeList</param>
        /// <param name="e">MouseEventArgs</param>
        /// <param name="menu">PopupMenu</param>
        /// <param name="attachMenuRule">AttachMenuRule</param>
        public static void AttachMenu(this TreeList tree, MouseEventArgs e, PopupMenu menu, Func<TreeListNode, bool> attachMenuRule)
        {
            if (tree == null)
                throw new ArgumentNullException("tree");
            if (menu == null)
                throw new ArgumentNullException("menu");
            if (e.Button == MouseButtons.Right && Control.ModifierKeys == Keys.None && tree.State == TreeListState.Regular)
            {
                Point _point = new Point(Cursor.Position.X, Cursor.Position.Y);
                TreeListHitInfo _hitInfo = tree.CalcHitInfo(e.Location);
                if (_hitInfo.HitInfoType == HitInfoType.Cell)
                    tree.SetFocusedNode(_hitInfo.Node);
                if (attachMenuRule(tree.FocusedNode))
                    menu.ShowPopup(_point);
            }
        }
        /// <summary>
        /// 设置父节点的状态AfterCheckNode(object sender, NodeEventArgs e)
        /// </summary>
        /// <param name="node"></param>
        /// <param name="check"></param>
        public static void ProcessNodeCheckState(this TreeListNode node, CheckState check)
        {
            if (node == null)
                throw new ArgumentNullException("node");
            SetCheckedChildNodes(node, check);
            SetCheckedParentNodes(node, check);
        }
        /// <summary>
        /// 设置子节点CheckState
        /// </summary>
        /// <param name="node"></param>
        /// <param name="check"></param>
        private static void SetCheckedChildNodes(TreeListNode node, CheckState check)
        {
            if (node != null)
            {
                node.LoopTreeNodes((TreeListNode _node) =>
                {
                    _node.CheckState = check;
                });
            }
        }
        /// <summary>
        /// 设置父节点CheckState
        /// </summary>
        /// <param name="node"></param>
        /// <param name="check"></param>
        private static void SetCheckedParentNodes(TreeListNode node, CheckState check)
        {
            if (node.ParentNode != null)
            {
                bool _checkStatus = false;
                CheckState _nodeState;
                node.LoopTreeNodes_Break((TreeListNode _node) =>
                {
                    _nodeState = _node.CheckState;
                    if (!check.Equals(_nodeState))
                    {
                        _checkStatus = !_checkStatus;
                        return false;//跳出循环
                    }
                    return true;//继续循环
                });
                node.ParentNode.CheckState = _checkStatus ? CheckState.Indeterminate : check;
                SetCheckedParentNodes(node.ParentNode, check);
            }
        }
        /// <summary>
        /// 根据CheckState获取TreeListNode
        /// </summary>
        /// <param name="tree">TreeList</param>
        /// <param name="state">CheckState</param>
        /// <param name="GetNodesByStateRule">返回True的时候继续</param>
        /// <returns>TreeListNode集合</returns>
        public static List<TreeListNode> GetNodesByState(this TreeList tree, CheckState state, Func<TreeListNode, bool> GetNodesByStateRule)
        {
            if (tree == null)
                throw new ArgumentNullException("tree");
            List<TreeListNode> _checkNodes = new List<TreeListNode>();
            tree.LoopTree((TreeListNode node) =>
            {
                if (GetNodesByStateRule(node))
                {
                    if (node.CheckState == state)
                        _checkNodes.Add(node);
                }
            });
            return _checkNodes;
        }

TreeNodeExtendMethods.zip

3.59 KB, 下载次数: 105

扩展方法文件

评分

参与人数 3贡献 +3 赞扬 +3 收起 理由
ytkfqsyskzhl + 1 Thanks
maple + 1 很给力
羽叶 + 3 + 1 赞一个

查看全部评分

回复

使用道具 举报

0

精华

1

贡献

0

赞扬

帖子
21
软币
128
在线时间
9 小时
注册时间
2014-12-12
发表于 2014-12-25 14:41:26 | 显示全部楼层
受教了,谢谢楼主。
回复

使用道具 举报

Archiver|手机版|小黑屋|开发者网 ( 苏ICP备08004430号-2 )
版权所有:南京韵文教育信息咨询有限公司

GMT+8, 2024-5-3 23:17

Powered by Discuz! X3.4

Copyright © 2001-2021, Tencent Cloud.

快速回复 返回顶部 返回列表