国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 編程 > C# > 正文

DevExpress之TreeList用法實例總結

2020-01-24 02:34:20
字體:
來源:轉載
供稿:網友

本文實例總結了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;    }  }}

本文實例備有詳盡的注釋,可以幫助大家更好的加以理解。

發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 昭平县| 阿合奇县| 萍乡市| 乾安县| 六盘水市| 浦城县| 八宿县| 开远市| 类乌齐县| 西藏| 安西县| 微山县| 宝山区| 繁峙县| 乐都县| 平江县| 河北区| 惠东县| 志丹县| 元朗区| 嵊泗县| 永泰县| 富顺县| 怀来县| 泰兴市| 荆州市| 富锦市| 原阳县| 罗山县| 阿坝| 和硕县| 唐河县| 德钦县| 竹溪县| 西青区| 任丘市| 仁布县| 康乐县| 中西区| 隆德县| 宾川县|