Devexpress TreeList选择父级联动
2015-10-21 10:33
204 查看
Treelist当显示复选框后,父级和子级的复选框没有关联,使用过程中很不便,如图所示
![](http://images2015.cnblogs.com/blog/462240/201510/462240-20151021111424552-1561716358.png)
自己给treelist添加父子级联动
在窗体加载的时候传入需要联动的treelist实例化即可
效果图:
![](http://images2015.cnblogs.com/blog/462240/201510/462240-20151021111429224-2084807712.png)
![](http://images2015.cnblogs.com/blog/462240/201510/462240-20151021111424552-1561716358.png)
自己给treelist添加父子级联动
/// <summary> /// 初始化TreeList,父子节点选择关联 /// </summary> public class TreeListInitial { TreeList Tree; /// <summary> /// 初始化TreeList,父子节点选择关联 /// </summary> /// <param name="tree"></param> public TreeListInitial(TreeList tree) { Tree = tree; tree.BeforeCheckNode += tree_BeforeCheckNode; tree.AfterCheckNode += tree_AfterCheckNode; } public bool AllowCheck = true; public void tree_AfterCheckNode(object sender, DevExpress.XtraTreeList.NodeEventArgs e) { SetCheckedChildNodes(e.Node, e.Node.CheckState); SetCheckedParentNodes(e.Node, e.Node.CheckState); } void tree_BeforeCheckNode(object sender, DevExpress.XtraTreeList.CheckNodeEventArgs e) { e.CanCheck = AllowCheck; e.State = (e.PrevState == CheckState.Checked ? CheckState.Unchecked : CheckState.Checked); } public void SetNodeCheckState(TreeListNode node, CheckState checkState) { Tree.SetNodeCheckState(node, CheckState.Checked); SetCheckedChildNodes(node, CheckState.Checked); SetCheckedParentNodes(node, CheckState.Checked); } void SetCheckedChildNodes(TreeListNode node, CheckState check) { for (int i = 0; i < node.Nodes.Count; i++) { node.Nodes[i].CheckState = check; SetCheckedChildNodes(node.Nodes[i], check); } } void SetCheckedParentNodes(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); } } }
在窗体加载的时候传入需要联动的treelist实例化即可
new TreeListInitial(tree_Module)
效果图:
![](http://images2015.cnblogs.com/blog/462240/201510/462240-20151021111429224-2084807712.png)
相关文章推荐
- [Gearman] Gearman的问题分析与深入研究
- Error filling print... java.lang.String cannot be cast to java.lang.Number
- CXF 添加soap 头部信息
- 第三章 Data语意学
- CSS3 伪类中content中的内容
- C语言之指针
- 【笔记】两个修复网络的命令
- iApp(12)墙纸计划
- 关于ifame的一些个简单问题
- 获取IE下载历史的具体实现
- css 选择器
- php数据库操作类
- memcached之java客户端:spymemcached与spring整合
- 34.Rotate Array
- Instant Client Downloads for Linux x86
- Java 代理,动态代理
- 获取IE下载历史的具体实现
- 修改placeholder文字颜色
- 你不知道的JavaScript--Item13 理解 prototype, getPrototypeOf 和__proto__
- poj2486 Apple Tree 树形DP