Visual Layer Overview(2)--Visual Tree

来源:互联网 发布:和讯软件下载 编辑:程序博客网 时间:2024/05/04 15:35
visual tree包含一个应用UI中所有的可视化的元素(visual elements)。既然一个可视化元素中包含永久化的描画信息,可以认为visual tree是一个包含组成显示设备上输出的所有渲染信息的场景图。这棵树是应用直接创建的可视化元素的一个集合,无论使用代码创建的还是用标记语言创建的。visual tree还包括通过模版扩展的元素。
下面的例子定义了一个StackPanel元素
XAML
<StackPanel>
 <Label>User name:</Label>
 <TextBox />
 <Button Click="OnClick">OK</Button>
</StackPanel>
 
如果枚举组成StackPanel元素的可见对象的例子,可以得到下图所示的层次结构:
 

Rendering Order

 
 
 
 
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>
如果要枚举组成DockPanellogical 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.