Logic tree and visual tree in WPF

Source: Internet
Author: User

This part is from chapter 3 of the upcoming New Book WPF unleashed. For more information about the logic tree, see the following pseudo-XAML code example:

<Window...>
<Stackpanel>
<Label> labeltext </lable>
</Stackpanel>
</WINDOW>

 

In such a simple UI, a window is a root node with a subnode stackpanel. Stackpanel has a sub-node label. Note that there is also a child node string (labeltext) under the label, which is also a leaf node. This constitutes a logic tree of the window. The logic tree always exists in the UI of WPF, regardless of whether the UI is written in XAML or code. Every aspect of WPF (attributes, events, resources, and so on) depends on the logic tree.

 

The visual tree is basically an extension of the logic tree. Each node of the logic tree is broken down into its core visual components. The node of the logic tree is basically a black box for us. Different visual trees expose visual implementation details. The following figure shows the visual tree structure of the above four lines of XAML code:

 

 

 

Not all logical Tree nodes can be extended to visual Tree nodes. Only elements inherited from system. Windows. Media. Visual and system. Windows. Media. visual3d can be included in the visual tree. Other elements cannot be included because they do not have their own rendering behavior.

 

Xamlpad in Windows Vista SDK tools provides the visual Tree display function. Note that xamlpad can only view the XAML document with page as the root element and the sizetocontent attribute is removed. As shown in:

 

 

Note the areas highlighted in the toolbar in the figure. We can see that visual tree is indeed complicated, and it also contains many invisible elements, such as contentpresenter. Although visual tree is complex, we generally do not need to pay too much attention to it. When fundamentally changing the style and appearance of controls, we need to pay attention to the use of visual tree, because in this case, we usually change the visual logic of controls.

 

WPF also provides helper classes for traversing the logical and visual trees: system. Windows. logicaltreehelper and system. Windows. Media. visualtreehelper. Pay attention to the traversal position. The logic tree can be traversed in the class constructor. However, the visual tree can only be formed after at least one layout. Therefore, it cannot be traversed in the constructor. This function is usually performed in oncontentrendered, which is called after the layout occurs.

 

In fact, each tree node element also contains the Traversal method. For example, the visual class contains three Member protection methods: visualparent, visualchildrencount, and getvisualchild. They allow access to the parent and child elements of visual. For frameworkelement, it usually defines a common parent attribute to indicate its logical parent element. A specific frameworkelement subclass exposes its logical child elements in different ways. For example, if the molecular element is a Children collection and sometimes the content attribute forces the element to have only one logical child element.

This article from the csdn blog, reproduced please indicate the source: http://blog.csdn.net/laiyiling/archive/2007/01/12/1481498.aspx

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.