在mobile里使用TreeView控件时,控件本身没有提供节点复选框选择判断逻辑。
如选中父节点,所有子节点全部选中;
取消父节点,同时也取消所有子节点;
或者选中所有子节点,父节点自动选中等等。
以下代码能够实现:
view plaincopy to clipboardprint?
- /// <summary>
- /// 复选框点击事件
- /// </summary>
- /// <param name="sender"></param>
- /// <param name="e"></param>
- private void tvPeople_AfterCheck(object sender, TreeViewEventArgs e)
- {
- if (e.Action != TreeViewAction.Unknown)
- {
- if (e.Node.Nodes.Count > 0)
- {
- this.CheckAllChildNodes(e.Node, e.Node.Checked);
- }
- CheckParentNodes(e.Node);
- }
- }
- /// <summary>
- /// 递归设置子节点
- /// </summary>
- /// <param name="treeNode">节点对象</param>
- /// <param name="nodeChecked">是否选择</param>
- private void CheckAllChildNodes(TreeNode treeNode, bool nodeChecked)
- {
- foreach (TreeNode node in treeNode.Nodes)
- {
- node.Checked = nodeChecked;
- if (node.Nodes.Count > 0)
- {
- this.CheckAllChildNodes(node, nodeChecked);
- }
- }
- }
- /// <summary>
- /// 递归设置父节点
- /// </summary>
- /// <param name="treeNode"></param>
- private void CheckParentNodes(TreeNode treeNode)
- {
- if (null != treeNode.Parent)
- {
- int count = treeNode.Parent.Nodes.Count;
- if (treeNode.Checked == true)
- {
- for (int i = 0; i < count; i++)
- {
- if (treeNode.Parent.Nodes[i].Checked == false)
- break;
- if (i == count - 1)
- treeNode.Parent.Checked = true;
- }
- }
- else
- {
- for (int i = 0; i < count; i++)
- {
- if (treeNode.Parent.Nodes[i].Checked == true)
- break;
- if (i == count - 1)
- treeNode.Parent.Checked = false;
- }
- }
- CheckParentNodes(treeNode.Parent);
- }
- }