DevExpress之TreeList用法实例总结

 更新时间:2014年08月07日 10:14:49   投稿:shichen2014  
这篇文章主要介绍了DevExpress之TreeList用法,对于C#初学者有一定的借鉴价值,需要的朋友可以参考下

本文实例总结了DevExpress之TreeList用法,希望对大家学习C#程序设计起到一定的帮助作用。具体实例如下:

using System;
using System.Collections.Generic;
using System.Drawing;
using System.Windows.Forms;
using DevExpress.XtraBars;
using DevExpress.XtraTreeList;
using DevExpress.XtraTreeList.Nodes;

namespace DevExpressUtilHelpV3
{
  public static class TreeListToolV3
  {
    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);
      }
    }
    public delegate bool CheckNodeRule(TreeListNode fucusedNode);
    public delegate void CheckNodeNullRule();
    /// <summary>
    /// 节点为null检查
    /// </summary>
    /// <param name="fucusedNode">TreeListNode</param>
    /// <param name="checkNodeRule">若为NULL,处理逻辑</param>
    /// <returns>TreeListNode</returns>
    public static TreeListNode CheckNull(this TreeListNode fucusedNode, CheckNodeNullRule checkNodeRule)
    {
      if (fucusedNode == null)
      {
        checkNodeRule();
        return null;
      }
      return fucusedNode;
    }
    /// <summary>
    /// 正对节点的检查逻辑
    /// </summary>
    /// <param name="fucusedNode">TreeListNode</param>
    /// <param name="checkNodeRule">检查逻辑代码[委托]</param>
    /// <returns>TreeListNode</returns>
    public static TreeListNode Check(this TreeListNode fucusedNode, CheckNodeRule checkNodeRule)
    {
      if (fucusedNode != null)
        return checkNodeRule(fucusedNode) == true ? fucusedNode : null;
      return null;
    }
    /// <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;
    }
  }
}

本文实例备有详尽的注释,可以帮助大家更好的加以理解。

相关文章

  • C# 基于udp广播收集局域网类所有设备信息

    C# 基于udp广播收集局域网类所有设备信息

    这篇文章主要介绍了C# 基于udp广播收集局域网类所有设备信息的方法,帮助大家更好的理解和使用c#,感兴趣的朋友可以了解下
    2020-12-12
  • C# using语法糖图文详解

    C# using语法糖图文详解

    这篇文章主要给大家介绍了关于C# using语法糖的相关资料,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
    2021-01-01
  • C#利用反射技术实现去掉按钮选中时的边框效果

    C#利用反射技术实现去掉按钮选中时的边框效果

    这篇文章主要介绍了C#利用反射技术实现去掉按钮选中时的边框效果,涉及C#针对窗口的参数设置技巧,具有一定参考借鉴价值,需要的朋友可以参考下
    2015-09-09
  • Unity实现游戏卡牌滚动效果

    Unity实现游戏卡牌滚动效果

    这篇文章主要为大家详细介绍了Unity实现游戏卡牌滚动效果,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
    2019-02-02
  • Unity UGUI的Outline描边组件的介绍使用示例

    Unity UGUI的Outline描边组件的介绍使用示例

    这篇文章主要介绍了Unity UGUI的Outline描边组件的介绍使用示例详解,有需要的朋友可以借鉴参考下,希望能够有所帮助,祝大家多多进步,早日升职加薪
    2023-08-08
  • C#如何添加PPT背景

    C#如何添加PPT背景

    这篇文章主要为大家详细介绍了C#如何添加PPT背景,添加纯色背景、渐变色背景、图片背景等,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
    2018-07-07
  • C#四舍五入MidpointRounding.AwayFromZero解析

    C#四舍五入MidpointRounding.AwayFromZero解析

    这篇文章主要介绍了C#四舍五入MidpointRounding.AwayFromZero,具有很好的参考价值,希望对大家有所帮助。如有错误或未考虑完全的地方,望不吝赐教
    2023-05-05
  • C#编程实现获取文件夹中所有文件的文件名

    C#编程实现获取文件夹中所有文件的文件名

    这篇文章主要介绍了C#编程实现获取文件夹中所有文件的文件名,可实现获取特定目录下制定类型文件名称的功能,涉及C#针对文件与目录的遍历、查询等操作相关技巧,具有一定参考借鉴价值,需要的朋友可以参考下
    2015-11-11
  • C#实现JWT无状态验证的实战应用解析

    C#实现JWT无状态验证的实战应用解析

    这篇文章主要介绍了C#实现JWT无状态验证的实战应用解析,本文给大家介绍的非常详细,对大家的学习或工作具有一定的参考借鉴价值,需要的朋友可以参考下
    2021-03-03
  • C#中Parallel类For、ForEach和Invoke使用介绍

    C#中Parallel类For、ForEach和Invoke使用介绍

    这篇文章介绍了C#中Parallel类For、ForEach和Invoke的使用方法,文中通过示例代码介绍的非常详细。对大家的学习或工作具有一定的参考借鉴价值,需要的朋友可以参考下
    2022-04-04

最新评论