網站首頁 編程語言 正文
一、什么是邏輯樹
邏輯樹就是描述WPF界面元素的實際構成,它是由程序在XAML中所有的UI元素組成。最顯著的特點就是由布局控件、或者其他常用的控件組成。
從上面的代碼中可以看出,Window、Grid、StackPanel、TextBox其實就是XAML界面的邏輯樹。
二、什么是可視樹
可視樹是由界面上可見的元素構成的,這些元素主要是由從Visual或者Visual3D類中派生出來的類。
上面代碼中的Window、Grid、StackPanel、TextBox它們本身就包含一些由Visual或者Visual3D類派生出的一些可視樹的元素來組成的。
三、邏輯樹和可視樹的遍歷
邏輯樹遍歷使用LogicalTreeHelper類。
可視樹遍歷使用VisualTreeHelper類。
演示遍歷邏輯樹和可視樹
1、XAML界面左邊顯示邏輯樹,右邊顯示可視樹,代碼如下:
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; } ////// 獲取邏輯樹 /// /// ///public static TreeViewItem GetLogicTree(DependencyObject obj) { if (obj == null) { return null; } //創建邏輯樹的節點 TreeViewItem treeItem = new TreeViewItem {Header=GetTypeDescription(obj),IsExpanded=true }; //循環遍歷,獲取邏輯樹的所有子節點 foreach (var child in LogicalTreeHelper.GetChildren(obj)) { //遞歸調用 var item = GetLogicTree(child as DependencyObject); if (item != null) { treeItem.Items.Add(item); } } return treeItem; } /// /// 獲取可視樹 /// /// ///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、在按鈕的點擊事件中將獲取的邏輯樹和可視樹添加到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 { ////// MainWindow.xaml 的交互邏輯 /// 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、點擊按鈕,界面運行效果
原文鏈接:https://www.cnblogs.com/dotnet261010/p/6367924.html
相關推薦
- 2022-12-23 C++?Boost?Conversion超詳細講解_C 語言
- 2022-12-29 React控制元素顯示隱藏的三種方法小結_React
- 2023-06-17 C語言string庫strcpy、strcmp、strcat函數的使用_C 語言
- 2022-04-01 k8s集群中移除節點后重新加入
- 2022-02-10 Kotlin實現Android系統懸浮窗詳解_Android
- 2023-08-16 使用el-row和el-col實現快速布局
- 2022-12-14 React?Native設備信息查看調試詳解_React
- 2022-06-21 Android?Studio實現下拉列表效果_Android
- 最近更新
-
- window11 系統安裝 yarn
- 超詳細win安裝深度學習環境2025年最新版(
- Linux 中運行的top命令 怎么退出?
- MySQL 中decimal 的用法? 存儲小
- get 、set 、toString 方法的使
- @Resource和 @Autowired注解
- Java基礎操作-- 運算符,流程控制 Flo
- 1. Int 和Integer 的區別,Jav
- spring @retryable不生效的一種
- Spring Security之認證信息的處理
- Spring Security之認證過濾器
- Spring Security概述快速入門
- Spring Security之配置體系
- 【SpringBoot】SpringCache
- Spring Security之基于方法配置權
- redisson分布式鎖中waittime的設
- maven:解決release錯誤:Artif
- restTemplate使用總結
- Spring Security之安全異常處理
- MybatisPlus優雅實現加密?
- Spring ioc容器與Bean的生命周期。
- 【探索SpringCloud】服務發現-Nac
- Spring Security之基于HttpR
- Redis 底層數據結構-簡單動態字符串(SD
- arthas操作spring被代理目標對象命令
- Spring中的單例模式應用詳解
- 聊聊消息隊列,發送消息的4種方式
- bootspring第三方資源配置管理
- GIT同步修改后的遠程分支