<Window>
<Grid>
<Label Content="Label" />
<Button Content="Button" />
</Grid>
</Window>
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. 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.
The Logical Tree
The logical tree describes the relations between elements of the user interface. The logical tree is responsible for:- Inherit DependencyProperty values
- Resolving
DynamicResources
references - Looking up element names for bindings
- 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:- Rendering visual elements
- Propagate element opacity
- Propagate Layout- and RenderTransforms
- Propagate the
IsEnabled
property. - Do Hit-Testing
- RelativeSource (FindAncestor)
No comments:
Post a Comment