WPF路由事件之邏輯樹和可視樹遍歷
一、什么是邏輯樹
邏輯樹就是描述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界面的邏輯樹。
二、什么是可視樹
可視樹是由界面上可見的元素構(gòu)成的,這些元素主要是由從Visual或者Visual3D類中派生出來的類。
上面代碼中的Window、Grid、StackPanel、TextBox它們本身就包含一些由Visual或者Visual3D類派生出的一些可視樹的元素來組成的。
三、邏輯樹和可視樹的遍歷
邏輯樹遍歷使用LogicalTreeHelper類。
可視樹遍歷使用VisualTreeHelper類。
演示遍歷邏輯樹和可視樹
1、XAML界面左邊顯示邏輯樹,右邊顯示可視樹,代碼如下:
<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="獲取邏輯樹和可視樹"></Button> <Grid> <Grid.ColumnDefinitions> <ColumnDefinition></ColumnDefinition> <ColumnDefinition></ColumnDefinition> </Grid.ColumnDefinitions> <DockPanel Grid.Column="0"> <TextBlock DockPanel.Dock="Top" Text="邏輯樹"></TextBlock> <TreeView Name="tvLogicTree"></TreeView> </DockPanel> <DockPanel Grid.Column="1"> <TextBlock DockPanel.Dock="Top" Text="可視樹"></TextBlock> <TreeView Name="tvVisualTree"></TreeView> </DockPanel> </Grid> </DockPanel> </Grid> </Window>
2、添加類,用于遍歷整個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.Media; namespace WpfRouteEvent { public class WpfTreeHelper { static string GetTypeDescription(object obj) { return obj.GetType().FullName; } /// <summary> /// 獲取邏輯樹 /// </summary> /// <param name="obj"></param> /// <returns></returns> public static TreeViewItem GetLogicTree(DependencyObject obj) { if (obj == null) { return null; } //創(chuàng)建邏輯樹的節(jié)點(diǎn) TreeViewItem treeItem = new TreeViewItem {Header=GetTypeDescription(obj),IsExpanded=true }; //循環(huán)遍歷,獲取邏輯樹的所有子節(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> /// 獲取可視樹 /// </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)擊事件中將獲取的邏輯樹和可視樹添加到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路由事件之邏輯樹和可視樹遍歷的文章就介紹到這了。希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
asp .net實(shí)現(xiàn)給圖片添加圖片水印方法示例
圖片上加水印相信每位程序員都會遇到這個需求,下面這篇文章主要給大家介紹了asp .net實(shí)現(xiàn)給圖片添加圖片水印的方法,文中給出了完整的實(shí)例代碼,相信對大家具有一定的參考價(jià)值,需要的朋友們下面來一起看看吧。2017-03-03ASP.NET?MVC實(shí)現(xiàn)登錄后跳轉(zhuǎn)到原界面
這篇文章介紹了ASP.NET?MVC實(shí)現(xiàn)登錄后跳轉(zhuǎn)到原界面的方法,文中通過示例代碼介紹的非常詳細(xì)。對大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2022-09-09.NET 6開發(fā)TodoList應(yīng)用引入數(shù)據(jù)存儲
這篇文章主要介紹了.NET 6開發(fā)TodoList應(yīng)用引入數(shù)據(jù)存儲,本篇文章僅完成了數(shù)據(jù)存儲服務(wù)的配置工作,目前還沒有添加任何實(shí)體對象和數(shù)據(jù)庫表定義,所以暫時(shí)沒有可視化的驗(yàn)證,僅我們可以運(yùn)行程序看我們的配置是否成功:下面來看詳細(xì)內(nèi)容吧2021-12-12關(guān)于visual studio 2012 update 2中的新功能介紹
本篇文章小編為大家介紹,關(guān)于visual studio 2012 update 2中的新功能介紹說明。需要的朋友參考下2013-04-04asp.net實(shí)現(xiàn)的計(jì)算網(wǎng)頁下載速度的代碼
剛看到有人給出asp.net實(shí)現(xiàn)的計(jì)算網(wǎng)頁下載速度的方法,本方法未經(jīng)本人測試,不知道能否可靠性如何。準(zhǔn)確來說,這只是個思路吧2013-03-03MVC4制作網(wǎng)站教程第三章 瀏覽用戶組操作3.1
這篇文章主要為大家詳細(xì)介紹了MVC4制作網(wǎng)站教程,瀏覽用戶組功能的實(shí)現(xiàn)代碼,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2016-08-08.net如何優(yōu)雅的使用EFCore實(shí)例詳解
這篇文章主要為大家介紹了.net如何優(yōu)雅的使用EFCore實(shí)例詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2022-11-11ASP.NET MVC實(shí)現(xiàn)橫向展示購物車
這篇文章介紹了ASP.NET MVC實(shí)現(xiàn)橫向展示購物車的方法,文中通過示例代碼介紹的非常詳細(xì)。對大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2022-09-09