WPF路由事件之邏輯樹(shù)和可視樹(shù)遍歷
一、什么是邏輯樹(shù)
邏輯樹(shù)就是描述WPF界面元素的實(shí)際構(gòu)成,它是由程序在XAML中所有的UI元素組成。最顯著的特點(diǎn)就是由布局控件、或者其他常用的控件組成。
<Window x:Class="WpfRouteEvent.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>
<StackPanel>
<TextBox></TextBox>
</StackPanel>
</Grid>
</Window>從上面的代碼中可以看出,Window、Grid、StackPanel、TextBox其實(shí)就是XAML界面的邏輯樹(shù)。
二、什么是可視樹(shù)
可視樹(shù)是由界面上可見(jiàn)的元素構(gòu)成的,這些元素主要是由從Visual或者Visual3D類中派生出來(lái)的類。
上面代碼中的Window、Grid、StackPanel、TextBox它們本身就包含一些由Visual或者Visual3D類派生出的一些可視樹(shù)的元素來(lái)組成的。
三、邏輯樹(shù)和可視樹(shù)的遍歷
邏輯樹(shù)遍歷使用LogicalTreeHelper類。
可視樹(shù)遍歷使用VisualTreeHelper類。
演示遍歷邏輯樹(shù)和可視樹(shù)
1、XAML界面左邊顯示邏輯樹(shù),右邊顯示可視樹(shù),代碼如下:
<Window x:Class="WpfRouteEvent.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>
<DockPanel>
<Button DockPanel.Dock="Top" Click="Button_Click" Content="獲取邏輯樹(shù)和可視樹(shù)"></Button>
<Grid>
<Grid.ColumnDefinitions>
<ColumnDefinition></ColumnDefinition>
<ColumnDefinition></ColumnDefinition>
</Grid.ColumnDefinitions>
<DockPanel Grid.Column="0">
<TextBlock DockPanel.Dock="Top" Text="邏輯樹(shù)"></TextBlock>
<TreeView Name="tvLogicTree"></TreeView>
</DockPanel>
<DockPanel Grid.Column="1">
<TextBlock DockPanel.Dock="Top" Text="可視樹(shù)"></TextBlock>
<TreeView Name="tvVisualTree"></TreeView>
</DockPanel>
</Grid>
</DockPanel>
</Grid>
</Window>2、添加類,用于遍歷整個(gè)XAML界面的邏輯樹(shù)和可視樹(shù)
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Media;
namespace WpfRouteEvent
{
public class WpfTreeHelper
{
static string GetTypeDescription(object obj)
{
return obj.GetType().FullName;
}
/// <summary>
/// 獲取邏輯樹(shù)
/// </summary>
/// <param name="obj"></param>
/// <returns></returns>
public static TreeViewItem GetLogicTree(DependencyObject obj)
{
if (obj == null)
{
return null;
}
//創(chuàng)建邏輯樹(shù)的節(jié)點(diǎn)
TreeViewItem treeItem = new TreeViewItem {Header=GetTypeDescription(obj),IsExpanded=true };
//循環(huán)遍歷,獲取邏輯樹(shù)的所有子節(jié)點(diǎn)
foreach (var child in LogicalTreeHelper.GetChildren(obj))
{
//遞歸調(diào)用
var item = GetLogicTree(child as DependencyObject);
if (item != null)
{
treeItem.Items.Add(item);
}
}
return treeItem;
}
/// <summary>
/// 獲取可視樹(shù)
/// </summary>
/// <param name="obj"></param>
/// <returns></returns>
public static TreeViewItem GetVisualTree(DependencyObject obj)
{
if (obj == null)
{
return null;
}
TreeViewItem treeItem = new TreeViewItem { Header=GetTypeDescription(obj),IsExpanded=true};
for (int i = 0; i < VisualTreeHelper.GetChildrenCount(obj); i++)
{
var child = VisualTreeHelper.GetChild(obj, i);
var item = GetVisualTree(child);
if (item != null)
{
treeItem.Items.Add(item);
}
}
return treeItem;
}
}
}3、在按鈕的點(diǎn)擊事件中將獲取的邏輯樹(shù)和可視樹(shù)添加到XAML界面中
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
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;
namespace WpfRouteEvent
{
/// <summary>
/// MainWindow.xaml 的交互邏輯
/// </summary>
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
}
private void Button_Click(object sender, RoutedEventArgs e)
{
this.tvLogicTree.Items.Add(WpfTreeHelper.GetLogicTree(this));
this.tvVisualTree.Items.Add(WpfTreeHelper.GetVisualTree(this));
}
}
}4、點(diǎn)擊按鈕,界面運(yùn)行效果

到此這篇關(guān)于WPF路由事件之邏輯樹(shù)和可視樹(shù)遍歷的文章就介紹到這了。希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
JWT + ASP.NET MVC時(shí)間戳防止重放攻擊詳解
這篇文章主要給大家介紹了關(guān)于JWT + ASP.NET MVC時(shí)間戳防止重放攻擊發(fā)的相關(guān)資料,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2018-07-07
Asp.net Core與類庫(kù)讀取配置文件信息的方法
這篇文章主要給大家介紹了關(guān)于Asp.net Core與類庫(kù)讀取配置文件信息的相關(guān)資料,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2018-12-12
關(guān)于vs2005、vs2008和vs2010項(xiàng)目互轉(zhuǎn)的總結(jié)
有做.net的程序員和朋友曾經(jīng)問(wèn)過(guò)我,關(guān)于vs2005和vs2008、vs2008和vs2010、vs2005和vs2010項(xiàng)目互轉(zhuǎn)的問(wèn)題,特整理下分享給大家2012-04-04
.NET運(yùn)行界面上,實(shí)現(xiàn)隨意拖動(dòng)控件的方法
.NET運(yùn)行界面上,實(shí)現(xiàn)隨意拖動(dòng)控件的方法,需要的朋友可以參考一下2013-03-03
ASP.NET網(wǎng)站聊天室的設(shè)計(jì)與實(shí)現(xiàn)(第3節(jié))
這篇文章主要介紹了ASP.NET網(wǎng)站聊天室的設(shè)計(jì)與實(shí)現(xiàn),了解Session、Application對(duì)象的屬性和事件,并且掌握利用它們?cè)陧?yè)面間保存和傳遞數(shù)據(jù)的方法,需要的朋友可以參考下2015-08-08

