1 ///treeLArea : TreeList;
2 ///imglChecked : ImageList;
3 ///
4 ///
5
6 //指定StateImageList
7 this.treelArea.StateImageList = this.imglChecked;
8
9 //treelArea的GetStateImage事件处理程序
10 private void treelArea_GetStateImage(object sender, DevExpress.XtraTreeList.GetStateImageEventArgs e)
11 {
12 try
13 {
14 CheckState checkState = (CheckState)e.Node.GetValue("checkState");
15 e.NodeImageIndex = (checkState == CheckState.Unchecked) ? 0 : (checkState == CheckState.Checked ? 1 : 2);
16 }
17 catch(Exception)
18 {
19
20 }
21
22 }
23
24 //鼠标单击事件处理
25 private void treelArea_MouseDown(object sender, MouseEventArgs e)
26 {
27 if (e.Button==MouseButtons.Left)
28 {
29 TreeListHitInfo hitInfo = (sender as TreeList).CalcHitInfo(new Point(e.X,e.Y));
30 if (hitInfo.HitInfoType==HitInfoType.StateImage)
31 {
32 SetCheckValue(hitInfo.Node);
33 }
34 }
35 }
36
37
38 //设置节点状态
39 private void SetCheckValue(TreeListNode node)
40 {
41 CheckState checkState = CheckState.Unchecked;
42 if ((CheckState)node.GetValue("checkState")==CheckState.Unchecked||(CheckState)node.GetValue("checkState")==CheckState.Indeterminate)
43 {
44 checkState = CheckState.Checked;
45 }
46 node.TreeList.FocusedNode = node;
47 node.TreeList.BeginUpdate();
48 node.SetValue("checkState",checkState);
49 SetParentCheckState(node,checkState);
50 SetchildCheckState(node, checkState);
51 node.TreeList.EndUpdate();
52 }
53
54 //递归设置父节点
55 private void SetParentCheckState(TreeListNode node,CheckState checkState)
56 {
57 TreeListNode parentNode = node.ParentNode;
58 bool style = false;
59 if (parentNode!=null)
60 {
61 foreach (TreeListNode n in parentNode.Nodes)
62 {
63 if (!checkState.Equals((CheckState)n.GetValue("checkState")))
64 {
65 style = true;
66 break;
67 }
68 }
69 parentNode.SetValue("checkState", style ? CheckState.Indeterminate : checkState);
70 SetParentCheckState(parentNode,checkState);
71 }
72
73 }
74
75 //递归设置子节点
76 private void SetchildCheckState(TreeListNode node, CheckState checkState)
77 {
78 if (node.HasChildren)
79 {
80 foreach (TreeListNode n in node.Nodes)
81 {
82 n.SetValue("checkState",checkState);
83 SetchildCheckState(n,checkState);
84 }
85 }
86
87 }
2 ///imglChecked : ImageList;
3 ///
4 ///
5
6 //指定StateImageList
7 this.treelArea.StateImageList = this.imglChecked;
8
9 //treelArea的GetStateImage事件处理程序
10 private void treelArea_GetStateImage(object sender, DevExpress.XtraTreeList.GetStateImageEventArgs e)
11 {
12 try
13 {
14 CheckState checkState = (CheckState)e.Node.GetValue("checkState");
15 e.NodeImageIndex = (checkState == CheckState.Unchecked) ? 0 : (checkState == CheckState.Checked ? 1 : 2);
16 }
17 catch(Exception)
18 {
19
20 }
21
22 }
23
24 //鼠标单击事件处理
25 private void treelArea_MouseDown(object sender, MouseEventArgs e)
26 {
27 if (e.Button==MouseButtons.Left)
28 {
29 TreeListHitInfo hitInfo = (sender as TreeList).CalcHitInfo(new Point(e.X,e.Y));
30 if (hitInfo.HitInfoType==HitInfoType.StateImage)
31 {
32 SetCheckValue(hitInfo.Node);
33 }
34 }
35 }
36
37
38 //设置节点状态
39 private void SetCheckValue(TreeListNode node)
40 {
41 CheckState checkState = CheckState.Unchecked;
42 if ((CheckState)node.GetValue("checkState")==CheckState.Unchecked||(CheckState)node.GetValue("checkState")==CheckState.Indeterminate)
43 {
44 checkState = CheckState.Checked;
45 }
46 node.TreeList.FocusedNode = node;
47 node.TreeList.BeginUpdate();
48 node.SetValue("checkState",checkState);
49 SetParentCheckState(node,checkState);
50 SetchildCheckState(node, checkState);
51 node.TreeList.EndUpdate();
52 }
53
54 //递归设置父节点
55 private void SetParentCheckState(TreeListNode node,CheckState checkState)
56 {
57 TreeListNode parentNode = node.ParentNode;
58 bool style = false;
59 if (parentNode!=null)
60 {
61 foreach (TreeListNode n in parentNode.Nodes)
62 {
63 if (!checkState.Equals((CheckState)n.GetValue("checkState")))
64 {
65 style = true;
66 break;
67 }
68 }
69 parentNode.SetValue("checkState", style ? CheckState.Indeterminate : checkState);
70 SetParentCheckState(parentNode,checkState);
71 }
72
73 }
74
75 //递归设置子节点
76 private void SetchildCheckState(TreeListNode node, CheckState checkState)
77 {
78 if (node.HasChildren)
79 {
80 foreach (TreeListNode n in node.Nodes)
81 {
82 n.SetValue("checkState",checkState);
83 SetchildCheckState(n,checkState);
84 }
85 }
86
87 }