当前位置:  编程技术>.net/c#/asp.net

DevExpress之TreeList用法实例总结

    来源: 互联网  发布时间:2014-11-01

    本文导语:  本文实例总结了DevExpress之TreeList用法,希望对大家学习C#程序设计起到一定的帮助作用。具体实例如下: using System; using System.Collections.Generic; using System.Drawing; using System.Windows.Forms; using DevExpress.XtraBars; using DevExpress.XtraTreeLi...

本文实例总结了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);
    /// 
    /// 获取选中节点到根节点的所有信息
    /// 
    /// TreeListNode
    /// 列名称
    /// 规则委托
    /// 路径信息
    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);
    /// 
    /// 获取筛选节点到根节点的所有信息
    /// 
    /// TreeListNode
    /// 列名称
    /// 规则委托
    /// 规则委托
    /// 路径信息
    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;
    }
    /// 
    /// 递归遍历树节点
    /// 
    /// 
    /// 
    public static void LoopTree(this TreeList tree, Action opreateRule)
    {
      if (tree == null)
        throw new ArgumentNullException("tree");
      foreach (TreeListNode node in tree.Nodes)
      {
        opreateRule(node);
        if (node.Nodes.Count > 0)
        {
          LoopTreeNodes(node, opreateRule);
        }
      }
    }
    /// 
    /// 递归遍历TreeListNode节点
    /// 
    /// 
    /// 
    public static void LoopTreeNodes(this TreeListNode node, Action opreateRule)
    {
      if (node == null)
        throw new ArgumentNullException("node");
      foreach (TreeListNode _childNode in node.Nodes)
      {
        opreateRule(_childNode);
        LoopTreeNodes(_childNode, opreateRule);
      }
    }
    /// 
    /// 递归遍历TreeListNode,当opreateRule返回false停止循环
    /// 
    /// TreeListNode
    /// Func
    public static void LoopTreeNodes_Break(this TreeListNode node, Func opreateRule)
    {
      if (node == null)
        throw new ArgumentNullException("node");
      foreach (TreeListNode _childNode in node.Nodes)
      {
        if (!opreateRule(_childNode))
          break;
        LoopTreeNodes_Break(_childNode, opreateRule);
      }
    }
    /// 
    /// 递归遍历TreeListNode,当opreateRule返回false跳出循环,直接进入下次循环
    /// 
    /// TreeListNode
    /// Func
    public static void LoopTreeNodes_Continue(this TreeListNode node, Func 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();
    /// 
    /// 节点为null检查
    /// 
    /// TreeListNode
    /// 若为NULL,处理逻辑
    /// TreeListNode
    public static TreeListNode CheckNull(this TreeListNode fucusedNode, CheckNodeNullRule checkNodeRule)
    {
      if (fucusedNode == null)
      {
        checkNodeRule();
        return null;
      }
      return fucusedNode;
    }
    /// 
    /// 正对节点的检查逻辑
    /// 
    /// TreeListNode
    /// 检查逻辑代码[委托]
    /// TreeListNode
    public static TreeListNode Check(this TreeListNode fucusedNode, CheckNodeRule checkNodeRule)
    {
      if (fucusedNode != null)
        return checkNodeRule(fucusedNode) == true ? fucusedNode : null;
      return null;
    }
    /// 
    /// 水平滚动条
    /// 
    /// TreeList
    public static void HorzScroll(this TreeList tree)
    {
      if (tree == null)
        throw new ArgumentNullException("tree");
      tree.OptionsView.AutoWidth = false;
      tree.BestFitColumns();
      tree.HorzScrollVisibility = ScrollVisibility.Always;
    }
    /// 
    /// 为TreeList附加右键菜单
    /// MouseUp(object sender, MouseEventArgs e)事件中调用
    /// 
    /// TreeList
    /// MouseEventArgs
    /// PopupMenu
    /// AttachMenuRule
    public static void AttachMenu(this TreeList tree, MouseEventArgs e, PopupMenu menu, Func 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);
      }
    }
    /// 
    /// 设置父节点的状态AfterCheckNode(object sender, NodeEventArgs e)
    /// 
    /// 
    /// 
    public static void ProcessNodeCheckState(this TreeListNode node, CheckState check)
    {
      if (node == null)
        throw new ArgumentNullException("node");
      SetCheckedChildNodes(node, check);
      SetCheckedParentNodes(node, check);
    }
    /// 
    /// 设置子节点CheckState
    /// 
    /// 
    /// 
    private static void SetCheckedChildNodes(TreeListNode node, CheckState check)
    {
      if (node != null)
      {
        node.LoopTreeNodes((TreeListNode _node) =>
        {
          _node.CheckState = check;
        });
      }
    }
    /// 
    /// 设置父节点CheckState
    /// 
    /// 
    /// 
    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);
      }
    }
    /// 
    /// 根据CheckState获取TreeListNode
    /// 
    /// TreeList
    /// CheckState
    /// 返回True的时候继续
    /// TreeListNode集合
    public static List GetNodesByState(this TreeList tree, CheckState state, Func GetNodesByStateRule)
    {
      if (tree == null)
        throw new ArgumentNullException("tree");
      List _checkNodes = new List();
      tree.LoopTree((TreeListNode node) =>
      {
        if (GetNodesByStateRule(node))
        {
          if (node.CheckState == state)
            _checkNodes.Add(node);
        }
      });
      return _checkNodes;
    }
  }
}

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


    
 
 
 
本站(WWW.)旨在分享和传播互联网科技相关的资讯和技术,将尽最大努力为读者提供更好的信息聚合和浏览方式。
本站(WWW.)站内文章除注明原创外,均为转载、整理或搜集自网络。欢迎任何形式的转载,转载请注明出处。












  • 相关文章推荐
  • DevExpress实现GridControl显示Gif动画的方法
  • DevExpress实现GridControl删除所有行的方法
  • DevExpress实现根据行,列索引来获取RepositoryItem的方法
  • DevExpress实现为TextEdit设置水印文字的方法
  • DevExpress实现GridControl根据列选中一行
  • DevExpress实现GridView当无数据行时提示消息
  • DevExpress实现GridControl单元格编辑验证的方法
  • DevExpress实现TreeList按条件隐藏节点CheckBox的方法
  • DevExpress设置TreeList图片节点背景色的方法
  • DevExpress实现TreeList向上递归获取符合条件的父节点
  • DevExpress实现自定义TreeListNode的Tooltip的方法
  • DevExpress GridControl实现根据RowIndex和VisibleColumnsIndex来获取单元格值
  • DevExpress设置FocusedNode背景色的方法
  • DevExpress获取TreeList可视区域节点集合的实现方法
  • DevExpress实现GridControl列头绘制Checkbox的方法
  • DevExpress实现TreeList父子节点CheckState状态同步的方法
  • DevExpress获取节点下可视区域子节点集合的实现方法
  • DevExpress实现GridControl同步列头checkbox与列中checkbox状态
  • DevExpress实现禁用TreeListNode CheckBox的方法
  • DevExpress根据条件设置GridControl RepositoryItem是否可编辑


  • 站内导航:


    特别声明:169IT网站部分信息来自互联网,如果侵犯您的权利,请及时告知,本站将立即删除!

    ©2012-2021,,E-mail:www_#163.com(请将#改为@)

    浙ICP备11055608号-3