Logical and Visual Trees WPF in VB.NET

In this article I will share some important points about the Logical and Visual Trees in WPF.
  • 2141

Logical Trees: The logical tree represents the essential structure of your UI. It closely matches the elements you declare in XAML, and excludes most visual elements created internally to help render the elements you declaredAs a WPF programmer, you'll spend most of your time building the logical tree and then backing it up with event handling code. In fact, all of the features you've considered so far, work through the logical tree. Basically it describes the relations between elements of the user interface. The main responsibilities of the logical tree are:

  • Inherit DependencyProperty values

  • Resolving DynamicResources references

  • Looking up element names for bindings

  • Forwaring RoutedEvents

    tree.gif
     

Visual Trees: A visual tree is an expanded version of the logical tree. It breaks elements down into smaller pieces, it represents all of the elements in your UI which render to an output device. The visual tree is used for many things like rendering, event routing, locating resources (if an element has no logical parent), and more. It contains all logical elements including all visual elements of the template of each element, and the main responsiblilities of  visual tree are:

  • Rendering visual elements

  • Propagate element opacity

  • Propagate Layout- and RenderTransforms

  • Propagate the IsEnabled property

  • Do Hit-Testing

  • RelativeSource (FindAncestor)

    tree-2.gif
     

Example of an simple logical tree:

<Window x:Class="Window1"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="SimpleWindow" Height="300" Width="300"> 
    <StackPanel Margin="10">
        <Button Padding="5" Margin="10">Button one</Button>
        <Button Padding="5" Margin="10">Button two</Button>
        <Button Padding="5" Margin="10">Button three</Button>
    </StackPanel>
</Window>

Output Window

tree-1.gif
 

In visual tree representation the output of  above example looks like the below given figure: 

tree-3.gif
 

Conclusion

Hope after reading this article your idea will clear about the concept of  Logical and Visual Trees in WPF.

Categories

More Articles

© 2020 DotNetHeaven. All rights reserved.