Tuesday, 16 July 2013

WPF Logical and Visual Tree

Elements of a WPF user interface are hierarchically related. This relation is called the LogicalTree. The template of one element consists of multiple visual elements. This tree is called the VisualTree. WPF differs between those two trees, because for some problems you only need the logical elements and for other problems you want all elements.
        <Label Content="Label" />
        <Button Content="Button" />

Why do we need two different kind of trees?
A WPF control consists of multiple, more primitive controls. A button - for example - consists of a border, a rectangle and a content presenter. These controls are visual children of the button.
When WPF renders the button, the element itself has no appearance, but it iterates through the visual tree and renders the visual children of it. This hierarchical relation can also be used to do hit-testing, layout etc.
But sometimes you are not interested in the borders and rectangles of a controls template. Particularly because the template can be replaced, and so you should not relate on the visual tree structure! Because of that you want a more robust tree that only contains the "real" controls - and not all the template parts. And that is the eligibility for the logical tree.

The Logical Tree
The logical tree describes the relations between elements of the user interface. The logical tree is responsible for:
1)    Inherit DependencyProperty values.
2)    Resolving DynamicResources references.
3)    Looking up element names for bindings.
4)    Forwaring RoutedEvents.

The Visual Tree
The visual tree contains all logical elements including all visual elements of the template of each element. The visual tree is responsible for:
1)    Rendering visual elements.
2)    Propagate element opacity.
3)    Propagate Layout- and RenderTransforms.
4)    Propagate the IsEnabled property.
5)    Do Hit-Testing.
6)    RelativeSource (FindAncestor).

Programmatically Find an Ancestor in the Visual Tree
If you are a child element of a user interface and you want to access data from a parent element, but you don't know how many levels up that elements is, it's the best solution to navigate up the tree until it finds an element of the requested type.
This helper does exactly this. You can use almost the same code to navigate through the logical tree. 
public static class VisualTreeHelperExtensions
    public static T FindAncestor<T>(DependencyObject dependencyObject)
        where T : class
        DependencyObject target = dependencyObject;

            target = VisualTreeHelper.GetParent(target);
        while (target != null && !(target is T));

        return target as T;

The following example shows how to use the helper. It starts at this and navigates up the visual tree until it finds an element of type Grid. If the helper reaches the root element of the tree, it returns null. 
var grid = VisualTreeHelperExtensions.FindAncestor<Grid>(this);

No comments:

Post a Comment