visual tree
包含一个应用
UI
中所有的可视化的元素(
visual elements
)。既然一个可视化元素中包含永久化的描画信息,可以认为
visual tree
是一个包含组成显示设备上输出的所有渲染信息的场景图。这棵树是应用直接创建的可视化元素的一个集合,无论使用代码创建的还是用标记语言创建的。
visual tree
还包括通过模版扩展的元素。
下面的例子定义了一个StackPanel元素
XAML
<StackPanel>
<Label>User name:</Label>
<TextBox />
<Button Click="OnClick">OK</Button>
</StackPanel>
如果枚举组成
StackPanel
元素的可见对象的例子,可以得到下图所示的层次结构:
Root visual
是
visual tree
层次结构中最顶层的元素,在大多数应用程序中,
root visual
的基类通常是Window
或者NavigationWindow
。可是,如果在
Win32
应用中
hosting
可见对象的话,
root visual
就是
Win32 window
中最顶层的元素。
For more information, see Hosting Visual Objects in a Win32 Application.
Relationship to the Logical Tree
Windows Presentation Foundation
中的
logical tree
表示应用程序运行时的元素。虽然你不会直接的操作这个树,但是这种对应用的视角对于理解
property inheritance
和
event routing
是很有帮助的。与
visual tree
不同,
logical tree
可以表示不可见的
data objects
,例如:
ListItem
。在大多数情况下,
logical tree
几乎就是应用程序标记语言的近似映射。下面的代码显示了一个在标记语言中定义的
DockPanel
元素。
XAML
<DockPanel>
<ListBox>
<ListBoxItem>Dog</ListBoxItem>
<ListBoxItem>Cat</ListBoxItem>
<ListBoxItem>Fish</ListBoxItem>
</ListBox>
<Button Click="OnClick">OK</Button>
</DockPanel>
如果要枚举组成
DockPanel
的
logical objects
的话,将得到下图的层次结构
visual tree
和
logical tree
都与当前的应用的元素同步,反映任何元素的增加,删除或者修改。但是它们显示了应用的不同视图。与
visual tree, logical tree
并没有扩展控件的
ContentPresenter
元素。这意味着并没有
logical tree
和
visual tree
之间的一一对应。实际上,在同一对象上调用
LogicalTreeHelper
's GetChildren me
方法和
VisualTreeHelper
's GetChild
方法,将得到不同的结果。
For more information on the logical tree, see Element Tree.