WPF学习(4)逻辑树和可视树
前面几节说了一些WPF的基础,包括XAML和布局等。在接下来的几节,我们来说说WPF的核心概念,包括逻辑树和可视树、依赖对象和依赖属性、路由事件、命令这几个部分。本节介绍下逻辑树(Logical Tree)和可视树(Visual Tree)。
逻辑树和可视树
在WPF中,用户界面是由XAML来呈现的。粗略地讲,从宏观上看,叶子为布局组件和控件所组成的树既是逻辑树,从微观上看,将逻辑树的叶子再放大可看到其内部是由可视化组件(继承自Visual类)组成的,叶子为这些可视化组件组成的树既是可视树。
逻辑树
举个例子来说明:
<Window x:Class="WpfTreeDemo.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="MainWindow" Height="350" Width="525">
<Grid>
<Grid.RowDefinitions>
<RowDefinition Height="100" />
<RowDefinition Height="100" />
<RowDefinition Height="100" />
</Grid.RowDefinitions>
<TabControl>
<TabItem Header="第一页">
<TextBlock Text="This is first page" />
</TabItem>
<TabItem Header="第二页">
<TextBox Text="This is second page" />
</TabItem>
</TabControl>
<Button x:Name="btnOK" Grid.Row="1" Width="80" Height="80" Click="btnOK_Click">
<Button.Content>
<Image Source="/Images/photo.png" x:Name="imgPhoto"/>
</Button.Content>
</Button>
<ListView x:Name="lvStudents" Grid.Row="2">
<ListView.View>
<GridView>
<GridViewColumn Header="Index" DisplayMemberBinding="{Binding Index}" />
<GridViewColumn Header="Username" DisplayMemberBinding="{Binding Username}" />
<GridViewColumn Header="Age" DisplayMemberBinding="{Binding Age}"/>
</GridView>
</ListView.View>
</ListView>
</Grid>
</Window>
来看一下它的逻辑树:

层级感很强,这也正是XAML强大表现力的体现。如何来操作这棵树呢?最简单的方法当然是设置控件的name属性,然后在cs文件中根据name属性值来获取控件。WPF内置了一个LogicalTreeHelper类,我们可以通过它来遍历树,代码如下:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using System.Collections;
using Microsoft.Windows.Themes;
using System.Diagnostics; namespace WpfTreeDemo
{
/// <summary>
/// MainWindow.xaml 的交互逻辑
/// </summary>
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
List<Student> students = new List<Student>
{
new Student{Index=,Username="Jello",Age=},
new Student{Index=,Username="Taffy",Age=}
};
this.lvStudents.ItemsSource = students;
PrintLogicalTree(, this); }
private void btnOK_Click(object sender, RoutedEventArgs e)
{
MessageBox.Show("I am a button");
}
public void PrintLogicalTree(int depth, object obj)
{
Debug.WriteLine(new string(' ', depth) + obj);
if (!(obj is DependencyObject)) return;
foreach (var child in LogicalTreeHelper.GetChildren(obj as DependencyObject))
{
PrintLogicalTree(depth + , child);
}
}
}
public class Student
{
public int Index { get; set; }
public string Username { get; set; }
public int Age { get; set; }
}
}
Debug运行后可以在Debug输出窗口看到界面的逻辑树。
可视树
想要观察可视树,需要将控件“打碎”来看下,这里我们以TextBox为例,用Blend来“打碎”它,看看这个控件的内部结构:
<LinearGradientBrush x:Key="TextBoxBorder" EndPoint="0,20" MappingMode="Absolute" StartPoint="0,0">
<GradientStop Color="#ABADB3" Offset="0.05"/>
<GradientStop Color="#E2E3EA" Offset="0.07"/>
<GradientStop Color="#E3E9EF" Offset="1"/>
</LinearGradientBrush>
<Style x:Key="TextBoxStyle1" BasedOn="{x:Null}" TargetType="{x:Type TextBox}">
<Setter Property="Foreground" Value="{DynamicResource {x:Static SystemColors.ControlTextBrushKey}}"/>
<Setter Property="Background" Value="{DynamicResource {x:Static SystemColors.WindowBrushKey}}"/>
<Setter Property="BorderBrush" Value="{StaticResource TextBoxBorder}"/>
<Setter Property="BorderThickness" Value="1"/>
<Setter Property="Padding" Value="1"/>
<Setter Property="AllowDrop" Value="true"/>
<Setter Property="FocusVisualStyle" Value="{x:Null}"/>
<Setter Property="ScrollViewer.PanningMode" Value="VerticalFirst"/>
<Setter Property="Stylus.IsFlicksEnabled" Value="False"/>
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="{x:Type TextBox}">
<Microsoft_Windows_Themes:ListBoxChrome x:Name="Bd" BorderBrush="{TemplateBinding BorderBrush}" BorderThickness="{TemplateBinding BorderThickness}" Background="{TemplateBinding Background}" RenderMouseOver="{TemplateBinding IsMouseOver}" RenderFocused="{TemplateBinding IsKeyboardFocusWithin}" SnapsToDevicePixels="true">
<ScrollViewer x:Name="PART_ContentHost" SnapsToDevicePixels="{TemplateBinding SnapsToDevicePixels}"/>
</Microsoft_Windows_Themes:ListBoxChrome>
<ControlTemplate.Triggers>
<Trigger Property="IsEnabled" Value="false">
<Setter Property="Background" TargetName="Bd" Value="{DynamicResource {x:Static SystemColors.ControlBrushKey}}"/>
<Setter Property="Foreground" Value="{DynamicResource {x:Static SystemColors.GrayTextBrushKey}}"/>
</Trigger>
</ControlTemplate.Triggers>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
主要是ListBoxChrome及ScrollViewer构成的,这也很好理解,将TextBox的功能拆分来看,ListBoxChrome主要可以用来输入,ScrollViewer用于当内容过多时会有滚动条。这里通过name属性一般是不能直接获取控件的,需要借助VisualTreeHelper类和Visual中的方法来获取。也举个例子:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using System.Collections;
using Microsoft.Windows.Themes;
using System.Diagnostics; namespace WpfTreeDemo
{
/// <summary>
/// MainWindow.xaml 的交互逻辑
/// </summary>
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
List<Student> students = new List<Student>
{
new Student{Index=,Username="Jello",Age=},
new Student{Index=,Username="Taffy",Age=}
};
this.lvStudents.ItemsSource = students;
PrintLogicalTree(, this); }
protected override void OnContentRendered(EventArgs e)
{
base.OnContentRendered(e);
PrintVisualTree(, this);
}
private void btnOK_Click(object sender, RoutedEventArgs e)
{
MessageBox.Show("I am a button");
}
public void PrintLogicalTree(int depth, object obj)
{
Debug.WriteLine(new string(' ', depth) + obj);
if (!(obj is DependencyObject)) return;
foreach (var child in LogicalTreeHelper.GetChildren(obj as DependencyObject))
{
PrintLogicalTree(depth + , child);
}
}
public void PrintVisualTree(int depth, DependencyObject obj)
{
Debug.WriteLine(new string(' ', depth) + obj);
for (int i = ; i < VisualTreeHelper.GetChildrenCount(obj); i++)
{
PrintVisualTree(depth + , VisualTreeHelper.GetChild(obj, i));
}
}
}
public class Student
{
public int Index { get; set; }
public string Username { get; set; }
public int Age { get; set; }
}
}
之所以在OnContentRendered中调用了一次是因为可视树直到Window完成至少一次布局后才会有节点,否则是空的,而实例化发生在布局完成之前(可以获取逻辑树),
OnContentRendered调用发生在布局完成之后(可以获取可视树)。
这里需要注意几点:
1.并不是所有的元素(无与生俱来的呈现行为)都会出现可视树中,只有继承自Visual类或者Visual3D类的元素才会包含其中。
2.逻辑树是静态的,而可视树是动态的(当用户切换Theme是会改变)。
3.一般情况下,我们不需要考虑可视树,除非要进行控件重塑。
WPF学习(4)逻辑树和可视树的更多相关文章
- WPF路由事件一:逻辑树和可视树
一.什么是逻辑树 逻辑树就是描述WPF界面元素的实际构成,它是由程序在XAML中所有的UI元素组成.最显著的特点就是由布局控件.或者其他常用的控件组成. <Window x:Class=&quo ...
- wpf 逻辑树与可视化树
XAML天生就是用来呈现用户界面的,这是由于它具有层次化的特性.在WPF中,用户界面由一个对象树构建而成,这棵树叫作逻辑树.逻辑树的概念很直观,但是为什么要关注它呢?因为几乎WPF的每一方面(属性.事 ...
- WPF学习(6)路由事件
做过.net开发的朋友对于事件应该都不陌生.追溯历史,事件(Event)首先应用在Com和VB上,它是对在MFC中使用的烦琐的消息机制的一个封装,然后.net又继承了这种事件驱动机制,这种事件也叫.n ...
- WPF学习(5)依赖属性
今天我们来学习WPF一个比较重要的概念:依赖属性.这里推荐大家看看周永恒大哥的文章,讲的确实很不错.我理解的没那么深入,只能发表一下自己的浅见.提到依赖属性,不得不说我们经常使用的传统的.net属性, ...
- 【WPF学习】第五十八章 理解逻辑树和可视化树
在前面章节中,花费大量时间分析了窗口的内容模型——换句话说,研究了如何在其他元素中嵌套元素,进而构建完整的窗口. 例如,考虑下图中显示的一个非常简单的窗口,该窗口包含两个按钮.为创建该按钮,在窗口中嵌 ...
- WPF中的逻辑树和可视化树
WPF中的逻辑树是指XAML元素级别的嵌套关系,逻辑树中的节点对应着XAML中的元素. 为了方便地自定义控件模板,WPF在逻辑树的基础上进一步细化,形成了一个“可视化树(Visual Tree)”,树 ...
- WPF 中的逻辑树(Logical Tree)与可视化元素树(Visual Tree)
一.前言 WPF 中有两种"树":逻辑树(Logical Tree)和可视化元素树(Visual Tree). Logical Tree 最显著的特点就是它完全由布局组件和控件 ...
- WP8.1程序开发,可视树VisualTreeHelper类的使用
对于可视树的使用,很久之前就接触了, 一方面当时知识太浅根本看不懂,就放下没看了: 另一方面,也没用到,就没往这方面努力研究学习: 现在好了,遇到问题了,正好涉及到VisualTreeHelper的使 ...
- AI逻辑实现-取舍行为树还是状态机
AI逻辑实现-选择行为树还是状态机? 关注AI的朋友可能会看过赖勇浩翻译的<有限状态机时代终结的10大理由> ,里面谈到了状态机的诸多弊端.同时在ppt(附上下载地址)中述说了行为树的诸多 ...
随机推荐
- js 自定义方法 实现停留几秒 sleep
function sleep(numberMillis) { var now = new Date(); var exitTime = now.getTime() + numberMillis; wh ...
- pygame系列_箭刺Elephant游戏
这个游戏原名为:Chimp,我们可以到: http://www.pygame.org/docs/tut/chimp/ChimpLineByLine.html 获取到源码和详细的源码讲解 下面是我对游戏 ...
- _tkinter.TclError: no display name and no $DISPLAY environment variable
_tkinter.TclError: no display name and no $DISPLAY environment variable 这是在使用cocos2d-x的pluginx时遇到的一个 ...
- SVM-SVM概述
(一)SVM背景资料简介 支持向量机(Support Vector Machine)这是Cortes和Vapnik至1995首次提出,样本.非线性及高维模式识别中表现出很多特有的优势,并可以推广应用到 ...
- hihocoder第42周 k*N骨牌覆盖(状态dp+矩阵快速幂)
上周的3*N的骨牌,因为状态只有8中,所以我们可以手算出状态转移的矩阵 但是这周是k*N,状态矩阵不好手算,都是我们改成用程序自动生成一个状态转移的矩阵就行了,然后用这个矩阵进行快速幂即可 枚举枚举上 ...
- RMQ之ST算法
#include <stdio.h> #include <string.h> ; int a[N]; ]; inline int min(const int &a, c ...
- 日历的问题C语言,C++(boost),python,Javascript,Java和Matlab实现
今天看到一个很有意思的话题,例的标题叙述性描述,下面: 根据以下信息来计算1901年1月1至2000年12月31适逢星期日每个月的第一天的合伙人数量? a) 1900.1.1星期一 b) 1月,3 ...
- android studio下gradle与Git错误解决方法
Error: Gradle: Execution failed for task ':mytask' > A problem occurred starting process 'command ...
- 求pi 的公式
pi = 3.1415926..... 下面用c 语言来求解PI 现有公式 (pi*pi)/6 = 1 + 1/(2*2) + 1/(3*3) + ... + 1/(n*n); #include &l ...
- zookeeper集群的python代码测试
上一篇已经讲解了如何安装zookeeper的python客户端,接下来是我在网上搜到的例子,举例应用环境是: 1.当有两个或者多个服务运行,并且同意时间只有一个服务接受请求(工作),其他服务待命. 2 ...