欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

WPF路由事件之邏輯樹和可視樹遍歷

 更新時(shí)間:2022年02月25日 14:46:20   作者:.NET開發(fā)菜鳥  
本文詳細(xì)講解了WPF路由事件之邏輯樹和可視樹遍歷的方法,文中通過示例代碼介紹的非常詳細(xì)。對大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下

一、什么是邏輯樹

邏輯樹就是描述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操作EXCEL的總結(jié)篇

    ASP.NET操作EXCEL的總結(jié)篇

    今年有個系統(tǒng)的部分EXCEL的操作也讓我做,順便結(jié)合之前操作EXCEL的經(jīng)驗(yàn)作一下總結(jié),可能也算不上什么,對于絕大多數(shù)來說也沒什么技術(shù)含量,網(wǎng)上一搜一大把,但我想還是有必要總結(jié)一下
    2011-02-02
  • asp .net實(shí)現(xiàn)給圖片添加圖片水印方法示例

    asp .net實(shí)現(xiàn)給圖片添加圖片水印方法示例

    圖片上加水印相信每位程序員都會遇到這個需求,下面這篇文章主要給大家介紹了asp .net實(shí)現(xiàn)給圖片添加圖片水印的方法,文中給出了完整的實(shí)例代碼,相信對大家具有一定的參考價(jià)值,需要的朋友們下面來一起看看吧。
    2017-03-03
  • ASP.NET?MVC實(shí)現(xiàn)登錄后跳轉(zhuǎn)到原界面

    ASP.NET?MVC實(shí)現(xiàn)登錄后跳轉(zhuǎn)到原界面

    這篇文章介紹了ASP.NET?MVC實(shí)現(xiàn)登錄后跳轉(zhuǎn)到原界面的方法,文中通過示例代碼介紹的非常詳細(xì)。對大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下
    2022-09-09
  • asp.net 打印控件使用方法

    asp.net 打印控件使用方法

    做過很多的Web項(xiàng)目,大多數(shù)在打印頁面內(nèi)容的時(shí)候,采用的都是通過Javascript調(diào)用系統(tǒng)內(nèi)置的打印方法進(jìn)行打印,也就是調(diào)用 PrintControl.ExecWB(?,?)實(shí)現(xiàn)直接打印和打印預(yù)覽功能。
    2010-01-01
  • .NET 6開發(fā)TodoList應(yīng)用引入數(shù)據(jù)存儲

    .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中的新功能介紹

    本篇文章小編為大家介紹,關(guān)于visual studio 2012 update 2中的新功能介紹說明。需要的朋友參考下
    2013-04-04
  • asp.net實(shí)現(xiàn)的計(jì)算網(wǎng)頁下載速度的代碼

    asp.net實(shí)現(xiàn)的計(jì)算網(wǎng)頁下載速度的代碼

    剛看到有人給出asp.net實(shí)現(xiàn)的計(jì)算網(wǎng)頁下載速度的方法,本方法未經(jīng)本人測試,不知道能否可靠性如何。準(zhǔn)確來說,這只是個思路吧
    2013-03-03
  • MVC4制作網(wǎng)站教程第三章 瀏覽用戶組操作3.1

    MVC4制作網(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í)例詳解

    這篇文章主要為大家介紹了.net如何優(yōu)雅的使用EFCore實(shí)例詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪
    2022-11-11
  • ASP.NET MVC實(shí)現(xiàn)橫向展示購物車

    ASP.NET MVC實(shí)現(xiàn)橫向展示購物車

    這篇文章介紹了ASP.NET MVC實(shí)現(xiàn)橫向展示購物車的方法,文中通過示例代碼介紹的非常詳細(xì)。對大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下
    2022-09-09

最新評論