要求如下:
1:选择父节点后,子节点全部打钩;
2:选择子节点而不选择父节点,则从当前节点的父节点一直到根节点check框都是半选状态。
3:当子节点均未勾选时,设置父节点状态为:未选中状态
具体实现如下:
一、OptionView—ShowCheckBoxes属性为true,则每个节点前面自动增加一个check框。
二、设置OptionBehavior—AllIndeterminateCheckState属性为true,则check框可以出现半选状态。
具体代码显示如下:
#region 点击节点前 /// <summary> /// 点击节点前 /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private void treeList1_BeforeCheckNode(object sender, DevExpress.XtraTreeList.CheckNodeEventArgs e) { //MessageBox.Show("treeList1_BeforeCheckNode"); e.State = (e.PrevState == CheckState.Checked ? CheckState.Unchecked : CheckState.Checked); } #endregion #region 点击节点后 /// <summary> /// 点击节点后 /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private void treeList1_AfterCheckNode(object sender, DevExpress.XtraTreeList.NodeEventArgs e) { if (e.Node == null) { return; } System.Data.DataRowView rov = treeList1.GetDataRecordByNode(e.Node) as System.Data.DataRowView; if (e.Node.CheckState == CheckState.Indeterminate) { e.Node.CheckState = CheckState.Checked; } if (e.Node.CheckState == CheckState.Checked) { if (e.Node.HasChildren) { this.CheckNode(e.Node); } this.ValidParentIsChecked(e.Node); } else if (e.Node.CheckState == CheckState.Unchecked) { if (e.Node.HasChildren) { this.SetCheckedChildNodes(e.Node, CheckState.Unchecked); } //从根节点往下 查询 是否有打钩的子节点,如果有那么 父节点的 半选状态不变否则变为 不选择状态 this.ValidParentNodeIsCanleSel(e.Node); } } #endregion #region 节点操作集合 /// <summary> /// 选择某个节点及其子节点 /// </summary> /// <param name="node"></param> private void CheckNode(DevExpress.XtraTreeList.Nodes.TreeListNode node) { foreach (DevExpress.XtraTreeList.Nodes.TreeListNode cnode in node.Nodes) { if (cnode != null) { cnode.CheckState = CheckState.Checked; } if (cnode.HasChildren) { CheckNode(cnode); } } } /// <summary> /// 勾选子节点后,修改父节点的状态 /// </summary> /// <param name="node"></param> private void ValidParentIsChecked(DevExpress.XtraTreeList.Nodes.TreeListNode node) { if (node.ParentNode != null) { if (node.ParentNode.CheckState == CheckState.Unchecked) { node.ParentNode.CheckState = CheckState.Indeterminate; } else if (node.ParentNode.CheckState == CheckState.Indeterminate) { if (!this.ValidIsHasCheckChildNode(node))//某节点下子节点均未被选中时,修改其状态 { node.ParentNode.CheckState = CheckState.Unchecked; } } this.ValidParentIsChecked(node.ParentNode); } } /// <summary> /// 从该节点往下 查询 是否有打钩的子节点,如果有那么 父节点的 半选、选中状态不变否则变为 不选择状态 /// </summary> /// <param name="node"></param> private void ValidParentNodeIsCanleSel(DevExpress.XtraTreeList.Nodes.TreeListNode node) { bool isSel = false; if (node.ParentNode != null) { if (node.ParentNode.CheckState == CheckState.Indeterminate || node.ParentNode.CheckState == CheckState.Checked) { //如果父节点的 状态为 半选 状态 这 根据父节点 判断子节点是否打钩 isSel = ValidIsHasCheckChildNode(node.ParentNode); if (isSel == false) { //如果所有的 子节点 都没有 “选中”那么 父节点的状态 变为 非选中状态 node.ParentNode.CheckState = CheckState.Unchecked; } } this.ValidParentNodeIsCanleSel(node.ParentNode); } } /// <summary> /// 判断该节点下是否有节点被选中,如果有返回true /// </summary> /// <param name="node"></param> /// <returns></returns> private bool ValidIsHasCheckChildNode(DevExpress.XtraTreeList.Nodes.TreeListNode node) { bool isCheck = false; foreach (DevExpress.XtraTreeList.Nodes.TreeListNode cnode in node.Nodes) { if (cnode != null) { if (cnode.CheckState == CheckState.Checked) { isCheck = true; return isCheck; } } if (cnode.HasChildren) { isCheck = this.ValidIsHasCheckChildNode(cnode); if (isCheck == true) { return isCheck; } } } return isCheck; } #endregion #region 获取全部子节点 private void GetChildNodes(TreeListNode parentNode, List<TreeListNode> list) { if (parentNode == null) { return; } if (parentNode.Nodes.Count > 0) { foreach (TreeListNode node in parentNode.Nodes) { list.Add(node); if (node.Nodes.Count > 0) { GetChildNodes(node, list); } } } } #endregion #region 选择子节点时触发 /// <summary> /// 选择子节点时触发 /// </summary> /// <param name="node"></param> /// <param name="check"></param> private void SetCheckedChildNodes(TreeListNode node, CheckState check) { foreach (TreeListNode tn in node.Nodes) { tn.CheckState = check; if (tn.HasChildren) { SetCheckedChildNodes(tn, check); } } } #endregion #region 选择父节点时触发(暂时未用) /// <summary> /// 选择父节点时触发 /// </summary> /// <param name="node"></param> /// <param name="check"></param> private void SetCheckedParentNodes(TreeListNode node, CheckState check) { if (node.ParentNode != null) { bool b = false; CheckState state; foreach (TreeListNode tln in node.ParentNode.Nodes) { state = (CheckState)tln.CheckState; if (!check.Equals(state)) { b = !b; break; } } node.ParentNode.CheckState = b ? CheckState.Indeterminate : check; SetCheckedParentNodes(node.ParentNode, check); } } #endregion #region 判断此节点下的所有孩子节点是否选中 /// <summary> /// 判断此节点下的所有孩子节点是否选中 /// </summary> /// <param name="node"></param> /// <returns></returns> private Boolean IsChildsChecked(TreeListNode node) { foreach (TreeListNode tln in node.ParentNode.Nodes) { if (tln.CheckState == CheckState.Unchecked) { return false; } if (tln.HasChildren) { IsChildsChecked(tln); } } return true; } #endregion
以上代码参考:点击打开链接
如果只想实现如下要求:
1:选择父节点后,子节点全部打钩;
2:选择子节点而不选择父节点,则从当前节点的父节点一直到根节点check框都是半选状态。
实现及代码如下(代码摘自http://blog.sina.com.cn/s/blog_95cfa64601019wf0.html):
一、treelist绑定的数据源是数据集(dateset)中的表,而且必须给:(当前节点id)KeyFieldName和(上一级节点id)ParentFieldName 两个属性绑定字段。
二、设置treelist1(名称)中OptionView—ShowCheckBoxes属性为true,则每个节点前面自动增加一个check框。
三、设置OptionBehavior—AllIndeterminateCheckState属性为true,则check框可以出现半选状态。
四、代码
private void treeList1_AfterCheckNode(object sender, DevExpress.XtraTreeList.NodeEventArgs e) { if (e.Node == null) return; System.Data.DataRowView rov = treeList1.GetDataRecordByNode(e.Node) as System.Data.DataRowView; if (e.Node.CheckState == CheckState.Indeterminate) { e.Node.CheckState = CheckState.Checked; } if (e.Node.CheckState == CheckState.Checked) { if (e.Node.HasChildren) { CheckNode(e.Node); } ValidParentIsChecked(e.Node); } else if (e.Node.CheckState == CheckState.Unchecked) { if (e.Node.HasChildren) { ClearNode(e.Node); } //从根节点往下 查询 是否有打钩的子节点,如果有那么 父节点的 半选状态不变否则变为 不选择状态 ValidParentNodeIsCanleSel(e.Node); } } private void CheckNode(DevExpress.XtraTreeList.Nodes.TreeListNode node) { foreach (DevExpress.XtraTreeList.Nodes.TreeListNode cnode in node.Nodes) { if (cnode != null) { cnode.CheckState = CheckState.Checked; } if (cnode.HasChildren) { CheckNode(cnode); } } } private void ValidParentIsChecked(DevExpress.XtraTreeList.Nodes.TreeListNode node) { if (node.ParentNode != null) { if(node.ParentNode.CheckState!=CheckState.Checked) node.ParentNode.CheckState = CheckState.Indeterminate; ValidParentIsChecked(node.ParentNode); } } private void ValidParentNodeIsCanleSel(DevExpress.XtraTreeList.Nodes.TreeListNode node) { bool isSel = false; if (node.ParentNode != null) { if (node.ParentNode.CheckState == CheckState.Indeterminate) {//如果父节点的 状态为 半选 状态 这 更具父节点 判断子节点是否打钩 isSel = ValidIsHasCheckChildNode(node.ParentNode); if (isSel == false) {//如果所有的 子节点 都没有 “选中”那么 父节点的状态 变为 非选中状态 node.ParentNode.CheckState = CheckState.Unchecked; } } ValidParentNodeIsCanleSel(node.ParentNode); } } /// /// 判断 子节点 是否 有 状态为 “选中”状态 /// true 表示有 false 表示为 没有 /// /// /// private bool ValidIsHasCheckChildNode(DevExpress.XtraTreeList.Nodes.TreeListNode node) { bool isCheck = false; foreach (DevExpress.XtraTreeList.Nodes.TreeListNode cnode in node.Nodes) { if (cnode != null) { if (cnode.CheckState == CheckState.Checked) { isCheck = true; return isCheck; } } if (cnode.HasChildren) { isCheck = ValidIsHasCheckChildNode(cnode); if (isCheck == true) { return isCheck; } } } return isCheck; } //通过treelist的 node节点 可以得到当前节点对应的行 System.Data.DataRowView rov = treeList1.GetDataRecordByNode(node) as System.Data.DataRowView; //如果想让treelist的check框不可编辑,我用到的办法是:在“BeforeCheckNode”这个事件中给出判断 private void treeList1_BeforeCheckNode(object sender, DevExpress.XtraTreeList.CheckNodeEventArgs e) { //验证并判断 treelist 是否可编辑(默认的可编辑的) ---2010/09/06上海修改 if (ValidOrgTreeIsEdit == false) e.CanCheck = false; else { e.CanCheck = true; } } //如果想 一个 node 绑定是 name 字段,但是显示为:code+name 可以在(CustomDrawNodeCell)这个事件中写方法 private void TreeCat_CustomDrawNodeCell(object sender, DevExpress.XtraTreeList.CustomDrawNodeCellEventArgs e) { if (e.CellValue == null) return; if (e.Node == null) return; if (e.Column.Equals(name)) { if (e.CellValue.ToString().Trim() != string.Empty) { System.Data.DataRowView rov = treeList1.GetDataRecordByNode(e.Node) as System.Data.DataRowView; if (rov != null) { e.CellText = rov["code"].ToString().Trim() + " - " + rov["name"].ToString().Trim(); } } } }
时间: 2024-10-12 15:42:19