A股上市公司传智教育(股票代码 003032)旗下技术交流社区北京昌平校区

 找回密码
 加入黑马

QQ登录

只需一步,快速开始

© 陈君 金牌黑马   /  2014-8-26 22:48  /  6591 人查看  /  2 人回复  /   0 人收藏 转载请遵从CC协议 禁止商业使用本文

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

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

2 个回复

倒序浏览
写的很好
回复 使用道具 举报
学习了,写的不错
回复 使用道具 举报
您需要登录后才可以回帖 登录 | 加入黑马