WPF中的树:LogicalTree、VisualTree

WPF 中的树


逻辑树LogicalTree

逻辑树是在WPF框架级别定义的,这意味着与逻辑树操作最相关的WPF基本元素是FrameworkElementFrameworkContentElement

 

LogicalTreeHelper

Object->LogicalTreeHelper

提供用于查询逻辑树中的对象的静态帮助器方法。

LogicalTreeHelper 对于分析方案非常有用,在这种情况下,将以递归方式遍历逻辑树,并使用一致的方法检查各种父对象或子对象

LogicalTreeHelper方法
名称备注权限

BringIntoView

尝试使所请求的 UI 元素可见,并在目标上引发 RequestBringIntoView 事件以报告结果。public static

FindLogicalNode

尝试查找并返回具有指定名称的对象。 搜索从指定对象开始,并持续到逻辑树的子节点中。public static

GetChildren

通过处理逻辑树,返回指定对象的直接子对象的集合。public static

GetParent

通过处理逻辑树,返回指定对象的父对象。public static

 


可视化树 VisualTree

WPF 中除了逻辑树的概念,还存在可视化树的概念。 可视化树描述了可视化对象的结构。

 

VisualTreeHelper

Object->VisualTreeHelper

提供一些实用工具方法,用于执行涉及可视化树中的节点的常规任务。

VisualTreeHelper方法
名称备注权限

GetBitmapEffect

返回指定 BitmapEffect 的 Visual 值。public static

GetBitmapEffectInput

返回指定 BitmapEffectInput 的 Visual 值。public static

GetCacheMode

检索指定的 Visual 的缓存表示形式。public static

GetChild

返回指定父可视对象中位于指定集合索引位置的子可视对象。public static

GetChildrenCount

返回指定可视对象包含的子级个数。public static

GetClip

将指定的 Visual 的剪辑区域作为 Geometry 值返回。public static

GetContentBounds

返回 Visual 的已缓存的边界框矩形。public static

GetDescendantBounds

返回视觉对象所有后代的全部内容范围框的联合,其中包括 Visual 的内容范围框。public static

GetDpi

获取测量和呈现此视觉对象的 DPI 信息。public static

GetDrawing

返回指定的 Visual 的绘图内容。public static

GetEdgeMode

将指定的 Visual 的边缘模式作为 EdgeMode 值返回。public static

GetEffect

获取指定 Visual 的位图效果。public static

GetOffset

返回 Visual 的偏移量。public static

GetOpacity

返回 Visual 的不透明度。public static

GetOpacityMask

返回一个 Brush 值,该值表示 Visual 的不透明蒙板。public static

GetParent

返回表示视觉对象的父对象的 DependencyObject 值。public static

GetTransform

返回 Transform 的 Visual 值。public static

GetXSnappingGuidelines

返回 X 坐标(垂直)准线集合。public static

GetYSnappingGuidelines

返回 Y 坐标(水平)准线集合。public static

HitTest

返回命中测试的最顶层 Visual 对象。public static

SetRootDpi

更新视觉对象的 DPI 信息。 它仅能在没有父级的视觉对象上调用。public static

范例

<Window x:Class="WPFTreeDemo.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
        xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
        xmlns:local="clr-namespace:WPFTreeDemo"
        mc:Ignorable="d"
        Title="MainWindow" Height="450" Width="800">
    <Grid>
        <Grid.ColumnDefinitions>
            <ColumnDefinition/>
            <ColumnDefinition/>
        </Grid.ColumnDefinitions>
        <StackPanel Grid.Column="0">
            <Button Content="获取窗口逻辑树" Click="OnGetLogicalTree"/>
            <TreeView x:Name="trwLogicalTree"/>
        </StackPanel>
        <StackPanel Grid.Column="1">
            <Button Content="获取窗口可视化树" Click="OnGetVisualTree"/>
            <TreeView  x:Name="trwVisualTree"/>
        </StackPanel>
    </Grid>
</Window>
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 WPFTreeDemo
{
    /// <summary>
    /// MainWindow.xaml 的交互逻辑
    /// </summary>
    public partial class MainWindow : Window
    {
        public MainWindow()
        {
            InitializeComponent();
        }

        private void OnGetLogicalTree(object sender, RoutedEventArgs e)
        {
            TreeViewItem treeViewItem = new TreeViewItem() { Header = "Window的逻辑树" };
            treeViewItem.IsExpanded = true;
            trwLogicalTree.Items.Add(treeViewItem);
            ShowVisualTree(this as FrameworkElement, treeViewItem,10);
        }

        private void OnGetVisualTree(object sender, RoutedEventArgs e)
        {
            TreeViewItem treeViewItem = new TreeViewItem() { Header = "Window的可视化树" };
            treeViewItem.IsExpanded = true;
            trwVisualTree.Items.Add(treeViewItem);
            ShowLogicalTree(this as FrameworkElement, treeViewItem, 10);
        }

        private void ShowLogicalTree(FrameworkElement f, TreeViewItem t,int depth)
        {
            if ((f as FrameworkElement) == null|| depth<=0) return;
            depth--;
            foreach (var item in LogicalTreeHelper.GetChildren(f))
            {
                TreeViewItem treeViewItem = new TreeViewItem() { Header = item.GetType().Name, IsExpanded=true};
                t.Items.Add(treeViewItem);                  
                        ShowLogicalTree(item as FrameworkElement, treeViewItem, depth);             
            }
        }

        private void ShowVisualTree(FrameworkElement f, TreeViewItem t, int depth)
        {
            if ((f as FrameworkElement) == null || depth <= 0) return;
            depth--;
            for (int i = 0; i < VisualTreeHelper.GetChildrenCount(f as DependencyObject); i++)
            {
                Visual childVisual = (Visual)VisualTreeHelper.GetChild(f as DependencyObject, i);
                TreeViewItem treeViewItem = new TreeViewItem() { Header = childVisual.GetType().Name, IsExpanded = true };
                t.Items.Add(treeViewItem);                
                ShowVisualTree(childVisual as FrameworkElement,treeViewItem,depth);
            }
        }
    }
}

 

 

  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值