欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

DevExpress之TreeList用法实例总结

程序员文章站 2024-02-18 11:59:40
本文实例总结了devexpress之treelist用法,希望对大家学习c#程序设计起到一定的帮助作用。具体实例如下: using system; using...

本文实例总结了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;
    }
  }
}

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