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 }

 

你可能感兴趣的:(DevExpress)