DevExpress TreeList使用

  1 using System;
  2 using System.Collections.Generic;
  3 using System.Drawing;
  4 using System.Windows.Forms;
  5 using DevExpress.XtraBars;
  6 using DevExpress.XtraTreeList;
  7 using DevExpress.XtraTreeList.Nodes;
  8
  9 namespace DevExpressUtilHelpV3
 10 {
 11   public static class TreeListToolV3
 12   {
 13     public delegate string BuildPathRule(string nodeText, string fullPathInfo);
 14     /// <summary>
 15     /// 获取选中节点到根节点的所有信息
 16     /// </summary>
 17     /// <param name="focusedNode">TreeListNode</param>
 18     /// <param name="columnID">列名称</param>
 19     /// <param name="buildPathRule">规则委托</param>
 20     /// <returns>路径信息</returns>
 21     public static string FullPathInfo(this TreeListNode focusedNode, string columnID, BuildPathRule buildPathRule)
 22     {
 23       if (focusedNode == null)
 24         throw new ArgumentNullException("focusedNode");
 25       if (string.IsNullOrEmpty("columnID"))
 26         throw new ArgumentNullException("columnID");
 27       string _fullPathInfo = string.Empty;
 28       _fullPathInfo = focusedNode.GetDisplayText(columnID);
 29       while (focusedNode.ParentNode != null)
 30       {
 31         focusedNode = focusedNode.ParentNode;
 32         string _nodeText = focusedNode.GetDisplayText(columnID).Trim();
 33         _fullPathInfo = buildPathRule(_nodeText, _fullPathInfo);
 34       }
 35       return _fullPathInfo;
 36     }
 37     public delegate bool CompareNodeRule(TreeListNode focusedNode);
 38     /// <summary>
 39     /// 获取筛选节点到根节点的所有信息
 40     /// </summary>
 41     /// <param name="focusedNode">TreeListNode</param>
 42     /// <param name="columnID">列名称</param>
 43     /// <param name="compareNodeRule">规则委托</param>
 44     /// <param name="buildPathRule">规则委托</param>
 45     /// <returns>路径信息</returns>
 46     public static string FilterPathInfo(this TreeListNode focusedNode, string columnID, CompareNodeRule compareNodeRule, BuildPathRule buildPathRule)
 47     {
 48       if (focusedNode == null)
 49         throw new ArgumentNullException("focusedNode");
 50       if (string.IsNullOrEmpty("columnID"))
 51         throw new ArgumentNullException("columnID");
 52       string _fullPathInfo = string.Empty;
 53       _fullPathInfo = focusedNode.GetDisplayText(columnID);
 54       while (focusedNode.ParentNode != null)
 55       {
 56         focusedNode = focusedNode.ParentNode;
 57         if (compareNodeRule(focusedNode))
 58         {
 59           string _nodeText = focusedNode.GetDisplayText(columnID).Trim();
 60           _fullPathInfo = buildPathRule(_nodeText, _fullPathInfo);
 61         }
 62       }
 63       return _fullPathInfo;
 64     }
 65     /// <summary>
 66     /// 递归遍历树节点
 67     /// </summary>
 68     /// <param name="tree"></param>
 69     /// <param name="opreateRule"></param>
 70     public static void LoopTree(this TreeList tree, Action<TreeListNode> opreateRule)
 71     {
 72       if (tree == null)
 73         throw new ArgumentNullException("tree");
 74       foreach (TreeListNode node in tree.Nodes)
 75       {
 76         opreateRule(node);
 77         if (node.Nodes.Count > 0)
 78         {
 79           LoopTreeNodes(node, opreateRule);
 80         }
 81       }
 82     }
 83     /// <summary>
 84     /// 递归遍历TreeListNode节点
 85     /// </summary>
 86     /// <param name="node"></param>
 87     /// <param name="opreateRule"></param>
 88     public static void LoopTreeNodes(this TreeListNode node, Action<TreeListNode> opreateRule)
 89     {
 90       if (node == null)
 91         throw new ArgumentNullException("node");
 92       foreach (TreeListNode _childNode in node.Nodes)
 93   {
 94   opreateRule(_childNode);
 95   LoopTreeNodes(_childNode, opreateRule);
 96   }
 97   }
 98 /// <summary>
 99 /// 递归遍历TreeListNode,当opreateRule返回false停止循环
100 /// </summary>
101 /// <param name="node">TreeListNode</param>
102 /// <param name="opreateRule">Func<TreeListNode, bool></param>
103 public static void LoopTreeNodes_Break(this TreeListNode node, Func<TreeListNode, bool> opreateRule)
104   {
105 if (node == null)
106 throw new ArgumentNullException("node");
107 foreach (TreeListNode _childNode in node.Nodes)
108   {
109 if (!opreateRule(_childNode))
110 break;
111   LoopTreeNodes_Break(_childNode, opreateRule);
112   }
113   }
114 /// <summary>
115 /// 递归遍历TreeListNode,当opreateRule返回false跳出循环,直接进入下次循环
116 /// </summary>
117 /// <param name="node">TreeListNode</param>
118 /// <param name="opreateRule">Func<TreeListNode, bool></param>
119 public static void LoopTreeNodes_Continue(this TreeListNode node, Func<TreeListNode, bool> opreateRule)
120   {
121 if (node == null)
122 throw new ArgumentNullException("node");
123 foreach (TreeListNode _childNode in node.Nodes)
124   {
125 if (!opreateRule(_childNode))
126 continue;
127   LoopTreeNodes_Continue(_childNode, opreateRule);
128   }
129   }
130 public delegate bool CheckNodeRule(TreeListNode fucusedNode);
131 public delegate void CheckNodeNullRule();
132 /// <summary>
133 /// 节点为null检查
134 /// </summary>
135 /// <param name="fucusedNode">TreeListNode</param>
136 /// <param name="checkNodeRule">若为NULL,处理逻辑</param>
137 /// <returns>TreeListNode</returns>
138 public static TreeListNode CheckNull(this TreeListNode fucusedNode, CheckNodeNullRule checkNodeRule)
139   {
140 if (fucusedNode == null)
141   {
142   checkNodeRule();
143 return null;
144   }
145 return fucusedNode;
146   }
147 /// <summary>
148 /// 正对节点的检查逻辑
149 /// </summary>
150 /// <param name="fucusedNode">TreeListNode</param>
151 /// <param name="checkNodeRule">检查逻辑代码[委托]</param>
152 /// <returns>TreeListNode</returns>
153 public static TreeListNode Check(this TreeListNode fucusedNode, CheckNodeRule checkNodeRule)
154   {
155 if (fucusedNode != null)
156 return checkNodeRule(fucusedNode) == true ? fucusedNode : null;
157 return null;
158   }
159 /// <summary>
160 /// 水平滚动条
161 /// </summary>
162 /// <param name="tree">TreeList</param>
163 public static void HorzScroll(this TreeList tree)
164   {
165 if (tree == null)
166 throw new ArgumentNullException("tree");
167   tree.OptionsView.AutoWidth = false;
168   tree.BestFitColumns();
169   tree.HorzScrollVisibility = ScrollVisibility.Always;
170   }
171 /// <summary>
172 /// 为TreeList附加右键菜单
173 /// MouseUp(object sender, MouseEventArgs e)事件中调用
174 /// </summary>
175 /// <param name="tree">TreeList</param>
176 /// <param name="e">MouseEventArgs</param>
177 /// <param name="menu">PopupMenu</param>
178 /// <param name="attachMenuRule">AttachMenuRule</param>
179 public static void AttachMenu(this TreeList tree, MouseEventArgs e, PopupMenu menu, Func<TreeListNode, bool> attachMenuRule)
180   {
181 if (tree == null)
182 throw new ArgumentNullException("tree");
183 if (menu == null)
184 throw new ArgumentNullException("menu");
185 if (e.Button == MouseButtons.Right && Control.ModifierKeys == Keys.None && tree.State == TreeListState.Regular)
186   {
187   Point _point = new Point(Cursor.Position.X, Cursor.Position.Y);
188   TreeListHitInfo _hitInfo = tree.CalcHitInfo(e.Location);
189 if (_hitInfo.HitInfoType == HitInfoType.Cell)
190   tree.SetFocusedNode(_hitInfo.Node);
191 if (attachMenuRule(tree.FocusedNode))
192   menu.ShowPopup(_point);
193   }
194   }
195 /// <summary>
196 /// 设置父节点的状态AfterCheckNode(object sender, NodeEventArgs e)
197 /// </summary>
198 /// <param name="node"></param>
199 /// <param name="check"></param>
200 public static void ProcessNodeCheckState(this TreeListNode node, CheckState check)
201   {
202 if (node == null)
203 throw new ArgumentNullException("node");
204   SetCheckedChildNodes(node, check);
205   SetCheckedParentNodes(node, check);
206   }
207 /// <summary>
208 /// 设置子节点CheckState
209 /// </summary>
210 /// <param name="node"></param>
211 /// <param name="check"></param>
212 private static void SetCheckedChildNodes(TreeListNode node, CheckState check)
213   {
214 if (node != null)
215   {
216   node.LoopTreeNodes((TreeListNode _node) =>
217   {
218   _node.CheckState = check;
219   });
220   }
221   }
222 /// <summary>
223 /// 设置父节点CheckState
224 /// </summary>
225 /// <param name="node"></param>
226 /// <param name="check"></param>
227 private static void SetCheckedParentNodes(TreeListNode node, CheckState check)
228   {
229 if (node.ParentNode != null)
230   {
231 bool _checkStatus = false;
232   CheckState _nodeState;
233   node.LoopTreeNodes_Break((TreeListNode _node) =>
234   {
235   _nodeState = _node.CheckState;
236 if (!check.Equals(_nodeState))
237   {
238   _checkStatus = !_checkStatus;
239 return false;//跳出循环
240   }
241 return true;//继续循环
242   });
243   node.ParentNode.CheckState = _checkStatus ? CheckState.Indeterminate : check;
244   SetCheckedParentNodes(node.ParentNode, check);
245   }
246   }
247 /// <summary>
248 /// 根据CheckState获取TreeListNode
249 /// </summary>
250 /// <param name="tree">TreeList</param>
251 /// <param name="state">CheckState</param>
252 /// <param name="GetNodesByStateRule">返回True的时候继续</param>
253 /// <returns>TreeListNode集合</returns>
254 public static List<TreeListNode> GetNodesByState(this TreeList tree, CheckState state, Func<TreeListNode, bool> GetNodesByStateRule)
255   {
256 if (tree == null)
257 throw new ArgumentNullException("tree");
258   List<TreeListNode> _checkNodes = new List<TreeListNode>();
259   tree.LoopTree((TreeListNode node) =>
260   {
261 if (GetNodesByStateRule(node))
262   {
263 if (node.CheckState == state)
264   _checkNodes.Add(node);
265   }
266   });
267 return _checkNodes;
268   }
269   }
270 }
时间: 2024-11-05 10:52:32

DevExpress TreeList使用的相关文章

DevExpress TreeList 那些事儿

1:TreeList绑定数据源 当我们给予TreeList 的 parentFieldName 和 KeyFieldName 两个属性之后 会自动的生成树结构. 1 var sql = @" SELECT * FROM TRAININGANDPROJECT"; 2 DataTable dt = SQLiteHelper.ExecuteTable(sql); 3 treeList1.DataSource = dt; // 将DataTable绑定到 TreeList中. 4 treeLi

[WinForm][DevExpress][TreeList]向上递归,获取公共父节点

最近项目开发中,需要获取到公共节点,如图: 譬如,当点击"Test103-2"节点,其类型是"灯"类型,那怎么获取到"中心区域"这个类型是"地域"的公共节点了?(不知道描述清楚木有哈) 核心代码: /// <summary> /// 向上递归,获取符合条件的父节点 /// </summary> /// <param name="node">需要向上递归的节点</pa

如何让DevExpress TreeList的每个结点高亮显示?

概述:如何让DevExpress TreeList的每个节点高亮显示? 如何让DXperience TreeList的每个节点高亮显示? 效果如下: private void treeList1_CustomDrawNodeCell(object sender, DevExpress.XtraTreeList.CustomDrawNodeCellEventArgs e)        {             TreeList node = sender as TreeList;       

DevExpress TreeList利用递归绑定数据

private void TreeListBind(DataTable dt, int p) { treeList1.Nodes.Clear(); if (dt.Rows.Count < 1) return; DataView dv = new DataView(dt); dv.RowFilter = "ParentFieldName=" + p; if (dv.Count < 1) return; TreeListNode Node = treeList1.AppendN

[WinForm][DevExpress][TreeList]节点互斥

关键代码: /// <summary> /// 节点互斥同步 /// 说明 /// eg: ///TreeListNode _node = e.Node; ///_node.SyncMutexNodeCheckState(_node.CheckState, n => n.GetNodeType() == NodeType.Cab); /// </summary> /// <param name="node">需要互斥同步的节点</para

[WinForm][DevExpress][TreeList]父子节点CheckState状态同步

关键代码: /// <summary> ///同步父子节点勾选状态 ///说明 ///在AfterCheckNode事件中使用代码 ///eg:e.Node.SyncNodeCheckState(e.Node.CheckState); /// </summary> /// <param name="node">需要同步的节点</param> /// <param name="check">节点当前勾选状态&

DevExpress TreeList添加右键菜单问题

添加右键流程: 1.先在窗体上拖两个控件,分别是popupMenu和barManager 2.barManager中绑定form属性为当前窗体名称 3.点击barManager右键选择customize,可直接添加子菜单,如果需要有工具栏.菜单栏.状态栏.怎选择Designer,可添加,然后在选择customize,添加command命令,再command选中菜单中执行命令 4.绑定右键菜单事件:在MouseDown事件中处理事件 --------------------------------

DevExpress TreeList使用教程之绑定多级树

DevExpress TreeList使用教程之绑定多级树 概述:TreeList控件可以同时显示树结构和其他数据列,即在一个列上建立父子关系展开或收缩,同时还可以显示其他列的内容.在TreeList中同时引入了 Node和 Columns概念, 第一列为树结构, 其余列为数据列, 任何列都可作为树结构列(即拖动到第一列)显示 TreeList控件可以同时显示树结构和其他数据列,即在一个列上建立父子关系展开或收缩,同时还可以显示其他列的内容. 在TreeList中同时引入了 Node和 Colu

[WinForm][DevExpress][TreeList]条件隐藏节点CheckBox

关键代码: /// <summary> /// 隐藏CheckBox /// 说明 /// 在CustomDrawNodeCheckBox事件中使用 /// eg: /// TreeList _curTree = (TreeList)sender; /// _curTree.HideCheckBox(n => n.GetNodeType() == NodeType.Area || n.GetNodeType() == NodeType.CabsGroupRoot, e); /// <

C# DevExpress TreeList指定KeyFieldName后无法显示该列的问题

可以在设置KeyFieldName前,将OptionsBehavior.PopulateServiceColumns属性设置为true,就会显示KeyFieldName,ParentFieldName字段 trlBM.DataSource = dtShow; trlBM.OptionsBehavior.PopulateServiceColumns = true; trlBM.KeyFieldName = "DEPTID"; trlBM.ParentFieldName = "P