[WinForm]DevExpress之TreeList使用小记

王朝学院·作者佚名  2016-08-27
窄屏简体版  字體: 小  |  中  |  大  |  超大  

usingSystem;usingSystem.Collections.Generic;usingSystem.Drawing;usingSystem.Windows.Forms;usingDevExPRess.XtraBars;usingDevExpress.XtraTreeList;usingDevExpress.XtraTreeList.Nodes;namespaceDevExpressUtilHelpV3

{publicstaticclassTreeListToolV3

{publicdelegatestringBuildPathRule(stringnodeText,stringfullPathInfo);/// <summary>/// 获取选中节点到根节点的所有信息/// </summary>/// <param name="focusedNode">TreeListNode</param>/// <param name="columnID">列名称</param>/// <param name="buildPathRule">规则委托</param>/// <returns>路径信息</returns>publicstaticstringFullPathInfo(thisTreeListNode focusedNode,stringcolumnID, BuildPathRule buildPathRule)

{if(focusedNode ==null)thrownewArgumentNullException("focusedNode");if(string.IsNullOrEmpty("columnID"))thrownewArgumentNullException("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;

}publicdelegateboolCompareNodeRule(TreeListNode focusedNode);/// <summary>/// 获取筛选节点到根节点的所有信息/// </summary>/// <param name="focusedNode">TreeListNode</param>/// <param name="columnID">列名称</param>/// <param name="compareNodeRule">规则委托</param>/// <param name="buildPathRule">规则委托</param>/// <returns>路径信息</returns>publicstaticstringFilterPathInfo(thisTreeListNode focusedNode,stringcolumnID, CompareNodeRule compareNodeRule, BuildPathRule buildPathRule)

{if(focusedNode ==null)thrownewArgumentNullException("focusedNode");if(string.IsNullOrEmpty("columnID"))thrownewArgumentNullException("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>publicstaticvoidLoopTree(thisTreeList tree, Action<TreeListNode> opreateRule)

{if(tree ==null)thrownewArgumentNullException("tree");foreach(TreeListNode nodeintree.Nodes)

{

opreateRule(node);if(node.Nodes.Count > 0)

{

LoopTreeNodes(node, opreateRule);

}

}

}/// <summary>/// 递归遍历TreeListNode节点/// </summary>/// <param name="node"></param>/// <param name="opreateRule"></param>publicstaticvoidLoopTreeNodes(thisTreeListNode node, Action<TreeListNode> opreateRule)

{if(node ==null)thrownewArgumentNullException("node");foreach(TreeListNode _childNodeinnode.Nodes)

{

opreateRule(_childNode);

LoopTreeNodes(_childNode, opreateRule);

}

}/// <summary>/// 递归遍历TreeListNode,当opreateRule返回false停止循环/// </summary>/// <param name="node">TreeListNode</param>/// <param name="opreateRule">Func<TreeListNode, bool></param>publicstaticvoidLoopTreeNodes_Break(thisTreeListNode node, Func<TreeListNode,bool> opreateRule)

{if(node ==null)thrownewArgumentNullException("node");foreach(TreeListNode _childNodeinnode.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>publicstaticvoidLoopTreeNodes_Continue(thisTreeListNode node, Func<TreeListNode,bool> opreateRule)

{if(node ==null)thrownewArgumentNullException("node");foreach(TreeListNode _childNodeinnode.Nodes)

{if(!opreateRule(_childNode))continue;

LoopTreeNodes_Continue(_childNode, opreateRule);

}

}publicdelegateboolCheckNodeRule(TreeListNode fucusedNode);publicdelegatevoidCheckNodeNullRule();/// <summary>/// 节点为null检查/// </summary>/// <param name="fucusedNode">TreeListNode</param>/// <param name="checkNodeRule">若为NULL,处理逻辑</param>/// <returns>TreeListNode</returns>publicstaticTreeListNode CheckNull(thisTreeListNode fucusedNode, CheckNodeNullRule checkNodeRule)

{if(fucusedNode ==null)

{

checkNodeRule();returnnull;

}returnfucusedNode;

}/// <summary>/// 正对节点的检查逻辑/// </summary>/// <param name="fucusedNode">TreeListNode</param>/// <param name="checkNodeRule">检查逻辑代码[委托]</param>/// <returns>TreeListNode</returns>publicstaticTreeListNode Check(thisTreeListNode fucusedNode, CheckNodeRule checkNodeRule)

{if(fucusedNode !=null)returncheckNodeRule(fucusedNode) ==true? fucusedNode :null;returnnull;

}/// <summary>/// 水平滚动条/// </summary>/// <param name="tree">TreeList</param>publicstaticvoidHorzScroll(thisTreeList tree)

{if(tree ==null)thrownewArgumentNullException("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>publicstaticvoidAttachMenu(thisTreeList tree, MouseEventArgs e, PopupMenu menu, Func<TreeListNode,bool> attachMenuRule)

{if(tree ==null)thrownewArgumentNullException("tree");if(menu ==null)thrownewArgumentNullException("menu");if(e.Button == MouseButtons.Right && Control.ModifierKeys == Keys.None && tree.State == TreeListState.Regular)

{

Point _point =newPoint(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>publicstaticvoidProcessNodeCheckState(thisTreeListNode node, CheckState check)

{if(node ==null)thrownewArgumentNullException("node");

SetCheckedChildNodes(node, check);

SetCheckedParentNodes(node, check);

}/// <summary>/// 设置子节点CheckState/// </summary>/// <param name="node"></param>/// <param name="check"></param>privatestaticvoidSetCheckedChildNodes(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>privatestaticvoidSetCheckedParentNodes(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;returnfalse;//跳出循环}returntrue;//继续循环});

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>publicstaticList<TreeListNode> GetNodesByState(thisTreeList tree, CheckState state, Func<TreeListNode,bool> GetNodesByStateRule)

{if(tree ==null)thrownewArgumentNullException("tree");

List<TreeListNode> _checkNodes =newList<TreeListNode>();

tree.LoopTree((TreeListNode node) =>

{if(GetNodesByStateRule(node))

{if(node.CheckState == state)

_checkNodes.Add(node);

}

});return_checkNodes;

}

}

}

 
 
免责声明:本文为网络用户发布,其观点仅代表作者个人观点,与本站无关,本站仅提供信息存储服务。文中陈述内容未经本站证实,其真实性、完整性、及时性本站不作任何保证或承诺,请读者仅作参考,并请自行核实相关内容。
 
© 2005- 王朝網路 版權所有 導航