This sample displays the visual tree of a Diagram. On the top is a simple example diagram, and on the bottom is the diagram showing the top diagram's visual tree.

The tree diagram is constructed by using a custom TreeModel that uses the actual visual elements of the visual tree used to render the diagram on the left. The VisualTreeModel overrides the FindChildNodeKeysForNode method so that it can use VisualTreeHelper methods to get the child visual elements for a UIElement.

Each node shows the name of the type of UIElement in the visual tree, using an ElementConverter that returns a string for the element.

Next to the name is a Button. The button's Click event handler toggles the value of Node.IsExpandedTree, and changes the appearance of the button. Note that only one of the two shapes, "MinusShape" and "PlusShape", is visible at any time.

But the button should only be visible when the node has visual children. This is achieved by binding the Visibility property of the button to the UIElement using a converter that calls VisualTreeHelper.GetChildrenCount to decide if it has any visual children.

Because the TreeLayout's performance conditions include VisibleChanged, after a node changes visibility another layout is performed.