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 > )
{
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;
}
}
}

最新文章

  1. BZOJ 2083: [Poi2010]Intelligence test
  2. VS2010统计代码行数 [转]
  3. 26 BasicUsageEnvironment基本使用环境——Live555源码阅读(三)UsageEnvironment
  4. String类型,Function类型
  5. [ASE][Daily Scrum]11.21
  6. Team them up!
  7. Wordpress Jigoshop插件路径泄露漏洞
  8. 项目报错-无法解析类型 XXXX.xx 从必需的 .class 文件间接引用了它
  9. LINUX系统安装MYSQL命令,纯手打
  10. js中的两个数字a,b求s=a+aa+aaa+aaaa+aa...a的值,其中a是一个数字。一共b个数字相加,例如用户输入2,5 s=2+22+222+2222+22222
  11. 【Xamarin挖墙脚系列:关闭 OS X El Capitan 中 SIP 安全设置功能】
  12. Scala并发编程
  13. 201521123057《Java程序设计》第14周学习总结
  14. Android Studio 字体和字号调整
  15. wget命令企业级应用参数详解
  16. Zookeeper运维问题集锦
  17. webpack的总结
  18. js 事件对象
  19. Postgres和MySQL创建用户并授予db权限
  20. MySQL中tinytext、text、mediumtext和longtext详解【转】

热门文章

  1. ubuntu16.04 安装composer和 laravel
  2. Ubuntu18.04 安装Chrome浏览器
  3. 转:Super Awesome Fuzzing, Part One
  4. 树形DP题目集合
  5. ZOJ 3211 Dream City
  6. 总结分析Java常见的四种引用
  7. zookeeper,hadoop安装部署其实与防火墙无关
  8. 编译 Windows 版本的 Unity Mono(2017-03-12 20:59)
  9. Codeforces Round #274 (Div. 2) Riding in a Lift(DP 前缀和)
  10. CSU - 1337 (搞笑版费马大定理 )