Devexpress TreeList控件的相关操作
/// <summary> /// 获取整棵树的所有选中的节点 /// </summary> /// <param name="treeList"></param> /// <returns></returns> private List<DNode.TreeListNode> GetSelectedNodes(DevExpress.XtraTreeList.TreeList treeList) { List<DNode.TreeListNode> sNodes = new List<DNode.TreeListNode>(); List<DNode.TreeListNode> allNodes = GetAllNode(treeList); foreach(DNode.TreeListNode node in allNodes) { if (node.Checked) sNodes.Add(node); } return sNodes; }
/// <summary> /// 获取node节点及其子孙节点的数据 /// </summary> /// <param name="node"></param> /// <param name="list"></param> private void GetAllDescendantData(DevExpress.XtraTreeList.Nodes.TreeListNode node, ref List<ObjectPermissionItem> list) { ObjectPermissionItem data = treeList1.GetDataRecordByNode(node) as ObjectPermissionItem; if (data != null) list.Add(data); if (node.Nodes.Count > 0) { foreach (DevExpress.XtraTreeList.Nodes.TreeListNode cnode in node.Nodes) GetAllDescendantData(cnode, ref list); } }
/// <summary> /// 获取treeList的所有节点 /// </summary> /// <param name="treeList"></param> /// <returns></returns> private List<DevExpress.XtraTreeList.Nodes.TreeListNode> GetAllNode(DevExpress.XtraTreeList.TreeList treeList) { List<DevExpress.XtraTreeList.Nodes.TreeListNode> allNodes = new List<DevExpress.XtraTreeList.Nodes.TreeListNode>(); foreach(DevExpress.XtraTreeList.Nodes.TreeListNode node in treeList.Nodes) { allNodes.Add(node); List<DNode.TreeListNode> cnode = new List<DNode.TreeListNode>(); DoGetNodes(ref cnode, node); allNodes.AddRange(cnode); } return allNodes; }
/// <summary> /// 递归获取parentNode节点的所有子孙节点 /// </summary> /// <param name="list"></param> /// <param name="parentNode"></param> private void DoGetNodes(ref List<DevExpress.XtraTreeList.Nodes.TreeListNode> list,DevExpress.XtraTreeList.Nodes.TreeListNode parentNode) { foreach(DNode.TreeListNode node in parentNode.Nodes) { list.Add(node); if(node.Nodes.Count>0) { DoGetNodes(ref list, node); } } }
/// <summary> /// 设置子节点的状态 /// </summary> /// <param name="node"></param> /// <param name="check"></param> private void SetCheckedChildNodes(DevExpress.XtraTreeList.Nodes.TreeListNode node, CheckState check) { for (int i = 0; i < node.Nodes.Count; i++) { node.Nodes[i].CheckState = check; SetCheckedChildNodes(node.Nodes[i], check); } } /// <summary> /// 设置父节点的状态 /// </summary> /// <param name="node"></param> /// <param name="check"></param> private void SetCheckedParentNodes(DevExpress.XtraTreeList.Nodes.TreeListNode node, CheckState check) { if (node.ParentNode != null) { bool b = false; CheckState state; for (int i = 0; i < node.ParentNode.Nodes.Count; i++) { state = (CheckState)node.ParentNode.Nodes[i].CheckState; if (!check.Equals(state)) { b = !b; break; } } node.ParentNode.CheckState = b ? CheckState.Indeterminate : check; SetCheckedParentNodes(node.ParentNode, check); } }
/// <summary> /// 点击一个节点,刷新其子节点和父节点的选中状态 /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private void treeList1_AfterCheckNode(object sender, DevExpress.XtraTreeList.NodeEventArgs e) { SetCheckedChildNodes(e.Node, e.Node.CheckState); SetCheckedParentNodes(e.Node, e.Node.CheckState); }