Vasu Balakrishnan’s Blog

WPF Trees

leave a comment »

WPF has the notion of Logical / Visual trees. The logical trees represents the control hierarchy as defined by the developer either via XAML or code. Just about every aspect of WPF (Properties, Events, Resources, etc) has behavior tied to the logical tree. Visual tree is an expansion of logical tree, in which nodes are broken down into their core visual components. The visual tree describes the structure of visuals (elements that inherit of Visual base class). Content elements (deriving from ContentElement) are not part of the Visual tree. Routed events travel along the visual tree and not the logical tree. Visual tree is of interest if you want low level control over drawing for performance / optimization. 

You should avoid writing code against the Visual tree as it breaks once of core principle of WPF – separation of look and logic. A Visual tree can change simply by a user switching to a different Windows theme. Visual tree is empty until the window undergoes layout at least once.

You can traverse Logical / Visual tree using LogicalTreeHelper / VisualTreeHelper classes.

Here is an article that goes further in explaining the trees.

Advertisements

Written by nbvasu

June 30, 2009 at 4:17 pm

Posted in WPF

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: