and visit all parent nodes of the selected node to re-evaluate their states based on the combined state of each parents children nodes.A Level-Order traversal algorithm beginning at the checked/unchecked node (either checking or unchecking the selected node and all its children),.We are now ready to summarize that the required algorithm needs to implement: Traversing the parents, however, is still required and yields the 'indeterminate' state for items a and b since their children do have different (checked and unchecked) states. The Level-Order traversal of the children of item x is not necessary since it has no children. The above algorithm is required to process a check operation on item x. But the children of nodes c and d are never required for a visit in this case since their cannot be a situation in which c or d would have to change its state. The algorithm would have to visit further parent nodes if item a would have a parent.
The algorithm can end here (frame 11) because node a does not have a parent node and the nodes c and d did not change their state. The correct state for item a is 'indeterminate' (yellow) since some of its children are checked (green) and some of its children are unchecked (black). The image in frame 7 shows that the system also visits the parent of a checked item (in this case the root item a to verify whether the state of the parent item is also consistent with the state of the newly checked item b The answer to this question can be determined by looking at the children c and d of item a in frames 8-9. Each later frame 2-6 shows how the system visits each child node and sets the checked state. The user clicks on node b and the system visits node b in frame 1 to set the checked state (green color -> checked). The above animation visualizes a tree view in which no item is checked in frame 0 (black color -> not checked). That is, the requested behavior requires a navigational concept that navigates also over the parents of a tree item (and not just over a current node and its children as pointed out earlier ). Requirement 8: All items in the tree should be in the expanded state by default.Ī careful read of these requirements (2 and 3) shows that we have to visit far more nodes in the tree to make sure that all details are full-filled. Requirement 7: Clicking on an item’s display text should select the item, but not toggle its check state. Requirement 6: Clicking on an item’s checkbox should toggle its check state, but not select the item. Requirement 5: Pressing the Spacebar or Enter keys should toggle the check state of the selected item. Requirement 4: Navigating from item to item should require only one press of an arrow key. Requirement 3: If an item’s descendants do not all have the same check state, that item’s check state must be ‘indeterminate.’ Requirement 2: Upon an item being checked or unchecked, all of its child items should be checked or unchecked, respectively. Requirement 1: Each item in the tree must display a checkbox that displays the text and check state of an underlying data object. Here is the list of requirements that Josh Smith, used to define this behavior in text: We can see here that checking a node does not only influence that nodes state but also the state of the parent and child items. The next screenshots below show the check state of all items in the tree view when the user checks the corresponding item (after starting the application):
The application starts-up with the first item selected and you can use the keyboard to navigate the tree and check or uncheck items using the space or enter key: The screenshots below give you an impression of the use case we are trying to solve here. I found the solution by Josh Smith and cannot really find anything wrong with it, other than that it is not in VB.Net and there are some minor details that I would describe slightly different to help the novice understanding the concept. This article is an attempt in describing an updated version with visual aids that should help the newbie to better understand how the solution works.Ī CodeProject reader asked to present a VB.Net solution that shows how check boxes can be used to manage items in a WPF Tree View. The WPF world has moved a little since then and there are some details we can implement slightly differently these days (mostly due to the work and findings that Josh Smith had contributed). The discussed solution is closely related to the article and solution described by Josh Smith in 2008, (which is almost 10 years ago). This article describes a solution to display and manage checkboxes in each tree view item of a WPF tree view.