/// <summary> /// 勾选点击触发 /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private void CheckBox_Click(object sender, RoutedEventArgs e) { TreeViewItem item = VisualUpwardSearch <TreeViewItem>(e.OriginalSource as DependencyObject) as TreeViewItem; if (item != null) { item.Focus(); if (tv_Tree.SelectedItem != null) { DeptNode tree = (DeptNode)tv_Tree.SelectedItem; if (lst_SelectedNodes.Find(s => s.Layer == tree.Layer) == null)//同一层级节点不能多选 { if (tree.IsChecked) { lst_SelectedNodes.Add(tree); } else { lst_SelectedNodes.Remove(tree); } SetChildrenChecked(tree, tree.IsChecked); //SetParentChecked(tree, tree.IsChecked);//暂不勾选父节点 } else { tree.IsChecked = false; return; } } e.Handled = true; } SetText(); }
/// <summary> /// 勾选子节点 /// </summary> /// <param name="node"></param> /// <param name="isChecked"></param> public void SetChildrenChecked(DeptNode node, bool isChecked) { foreach (DeptNode child in node.Nodes) { child.IsChecked = isChecked; if (isChecked) { lst_SelectedNodes.Add(child); } else { lst_SelectedNodes.Remove(child); } SetChildrenChecked(child, isChecked); } }
/// <summary> /// 勾选父节点 /// </summary> /// <param name="node"></param> /// <param name="isChecked"></param> public void SetParentChecked(DeptNode node, bool isChecked) { if (!string.IsNullOrEmpty(node.ParentDeptNo)) { DeptNode node_Parent = lst_DeptNode.Find(n => n.DeptNo == node.ParentDeptNo); if (node_Parent.Nodes.Find(n => n.IsChecked == true) != null)//若父节点的子节点有被勾选的 则勾选父节点 { node_Parent.IsChecked = true; SetParentChecked(node_Parent, isChecked); } else { node_Parent.IsChecked = false; SetParentChecked(node_Parent, isChecked); } } }