日本免费高清视频-国产福利视频导航-黄色在线播放国产-天天操天天操天天操天天操|www.shdianci.com

學無先后,達者為師

網站首頁 編程語言 正文

WPF路由事件之邏輯樹和可視樹遍歷_實用技巧

作者:.NET開發菜鳥 ? 更新時間: 2022-04-28 編程語言

一、什么是邏輯樹

邏輯樹就是描述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

欄目分類
最近更新