This is a mirror of official site: http://jasper-net.blogspot.com/

Logical Tree and Visual Tree in WPF

| Sunday, October 31, 2010
Elements of a WPF user interface are hierarchically related. This relation is called the Logical Tree. 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.

lognvis.bmp

<Window>
<Grid>
<Label Content="Label" />
<Button Content="Button" />
</Grid>
</Window>

a. Why do we need two different kinds 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. Particulary 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.

b. 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

c. The Visual Tree


Read more: C# Corner

Posted via email from .NET Info

0 comments: