Does in UltraTree exist a property which, when set properly, can check nodes using CheckBoxTriState automatically?
For example, when I check the parent, all of its children are checked. And afterwards, when I uncheck a child, the parent
becomes semichecked. Also, when I uncheck all of the children, the parent becomes unchecked.
I have the knowledge to implement such an algorithm using recursion but it takes time,
and if UltraTree has one implemented and optimized, I would like to use that one.
If not, I think that such an algorithm should be implemented in the future.
Many thanks, Marko.
FYI - this is a rather old post; since then, we have added automatic checkbox synchronization functionality as part of the control's intrinsic behavior (see Override.NodeStyle).
kevinshaffer said:Hopefully this will help someone else who struggles with making this work.
Thank's alot for sharing! :)
kevinshaffer said: Hopefully this will help someone else who struggles with making this work. I needed to have all nodes be able to be checked, not just nodes who have no children. Also, parent nodes should appear as indeterminate when some (but not all) of their children are checked. However, when a user checks a node that is indeterminate, it needs to go back to switching between checked and unchecked. The following is the code I came up with: private void treeView_AfterCheck(object sender, NodeEventArgs e) { if (e.TreeNode.CheckedState != CheckState.Indeterminate) { e.TreeNode.Override.NodeStyle = NodeStyle.CheckBox; foreach (UltraTreeNode childNode in e.TreeNode.Nodes) { childNode.CheckedState = e.TreeNode.CheckedState; } } if (e.TreeNode.Parent != null && e.TreeNode.Parent.CheckedState != e.TreeNode.CheckedState) { if (e.TreeNode.CheckedState == CheckState.Indeterminate) { e.TreeNode.Parent.CheckedState = CheckState.Indeterminate; } else { bool indeterminate = false; foreach (UltraTreeNode siblingNode in e.TreeNode.ParentNodesCollection) { if (siblingNode.CheckedState != e.TreeNode.CheckedState) { indeterminate = true; break; } } e.TreeNode.Parent.CheckedState = indeterminate ? CheckState.Indeterminate : e.TreeNode.CheckedState; } } } private void treeView_BeforeCheck(object sender, BeforeCheckEventArgs e) { if (e.NewValue == CheckState.Indeterminate && e.TreeNode.Override.NodeStyle != NodeStyle.CheckBoxTriState) { e.TreeNode.Override.NodeStyle = NodeStyle.CheckBoxTriState; } } Sorry for having to keep the lines so short so the post fits
Hopefully this will help someone else who struggles with making this work. I needed to have all nodes be able to be checked, not just nodes who have no children. Also, parent nodes should appear as indeterminate when some (but not all) of their children are checked. However, when a user checks a node that is indeterminate, it needs to go back to switching between checked and unchecked. The following is the code I came up with:
private void treeView_AfterCheck(object sender, NodeEventArgs e) { if (e.TreeNode.CheckedState != CheckState.Indeterminate) { e.TreeNode.Override.NodeStyle = NodeStyle.CheckBox; foreach (UltraTreeNode childNode in e.TreeNode.Nodes) { childNode.CheckedState = e.TreeNode.CheckedState; } } if (e.TreeNode.Parent != null && e.TreeNode.Parent.CheckedState != e.TreeNode.CheckedState) { if (e.TreeNode.CheckedState == CheckState.Indeterminate) { e.TreeNode.Parent.CheckedState = CheckState.Indeterminate; } else { bool indeterminate = false; foreach (UltraTreeNode siblingNode in e.TreeNode.ParentNodesCollection) { if (siblingNode.CheckedState != e.TreeNode.CheckedState) { indeterminate = true; break; } } e.TreeNode.Parent.CheckedState = indeterminate ? CheckState.Indeterminate : e.TreeNode.CheckedState; } } } private void treeView_BeforeCheck(object sender, BeforeCheckEventArgs e) { if (e.NewValue == CheckState.Indeterminate && e.TreeNode.Override.NodeStyle != NodeStyle.CheckBoxTriState) { e.TreeNode.Override.NodeStyle = NodeStyle.CheckBoxTriState; } }
Sorry for having to keep the lines so short so the post fits
thanks kevinshaffer, it was exactly what I needed !! well done !
Hello,
Here is and another approach for this functionality with recursive methods.
Please let me know if you have any further questions.