WPF 中的树


逻辑树LogicalTree

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

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);}}}
}

WPF中的树:LogicalTree、VisualTree相关推荐

  1. 理解WPF中的视觉树和逻辑树

    理解WPF中的视觉树和逻辑树  Understanding the Visual Tree and Logical Tree in WPF 这篇文章讨论WPF中视觉树和逻辑树的细微差别.同时提供了一个 ...

  2. 【转】WPF中关于样式和模板的区别

    1.WPF样式 类似于Web应用程序中的CSS,在WPF中可以为控件定义统一的样式(Style).样式属于资源的一种,例如为Button定义统一的背景颜色和字体: <Window.Resourc ...

  3. WPF中的ControlTemplate

    WPF中的ControlTemplate WPF包含数据模板和控件模板,其中控件模板又包括ControlTemplate和 ItemsPanelTemplate,这里讨论一下ControlTempla ...

  4. 了解 WPF 中的路由事件和命令

    目录 路由事件概述 WPF 元素树 事件路由 路由事件和组合 附加事件 路由命令概述 操作中的路由命令 命令路由 定义命令 命令插入 路由命令的局限 避免命令出错 超越路由命令 路由处理程序示例 要想 ...

  5. 在WPF中实现平滑滚动

    原文:在WPF中实现平滑滚动 WPF实现滚动条还是比较方便的,只要在控件外围加上ScrollViewer即可,但美中不足的是:滚动的时候没有动画效果.在滚动的时候添加过渡动画能给我们的软件增色不少,例 ...

  6. WPF中的Data Binding调试指南

    点击蓝字"大白技术控"关注我哟 加个"星标★",每日良时,好文必达! WPF中的Data Binding如何Debug? 大家平时做WPF开发,相信用Visua ...

  7. WPF中使用流文档灵活地显示内容

    WPF中使用流文档灵活地显示内容                             by: Markus Egger                             form: http ...

  8. 理解Windows窗体和WPF中的跨线程调用

    你曾开发过Windows窗体程序,可能会注意到有时事件处理程序将抛出InvalidOperationException异常,信息为" 跨线程调用非法:在非创建控件的线程上访问该控件" ...

  9. 【转】在WPF中自定义控件

    周银辉的开发博客(WPF) 在WPF中自定义控件(1) 一, 不一定需要自定义控件 在使用WPF以前,动辄使用自定义控件几乎成了惯性思维,比如需要一个带图片的按钮,但在WPF中此类任务却不需要如此大费 ...

最新文章

  1. SQLSERVER监控复制并使用数据库邮件功能发告警邮件
  2. Notepad++添加右键菜单
  3. stoi() 函数----将一个string类型转换为int型
  4. python线程暂停_关于多线程:如何使“停止”按钮终止已经在Tkinter中运行的“开始”功能(Python)...
  5. App设计灵感之十二组精美的音乐播放器App设计案例
  6. 老牌开源Office操作组件NPOI现已支持.NET Core
  7. android-铃声的设置与播放
  8. ABP入门系列(10)——扩展AbpSession
  9. REST framework 权限管理源码分析
  10. 如何编写有效的Bug Report
  11. linux服务器80端口是默认开放的么_「Linux」25节-防火墙iptables
  12. 入学年份 mysql_MySQl的学习1___概述
  13. 基于PID算法的水箱温度控制系统
  14. 带壳破解SMC补丁技术
  15. php上传文件时报错:only variables should be passed by reference
  16. 计算机音乐红色彼岸花,《抖音》红色彼岸花花瓣遍地撒是什么歌
  17. Unity项目 - 捡苹果 Apple Picker
  18. 仰天大笑出门去,我辈岂是蓬蒿人。
  19. Cython入门到放弃(一)
  20. 大数据三个特点的理解

热门文章

  1. ajax异步请求数据库实现级联下拉菜单。
  2. 北上深杭广漂,似曾相识的代码人生
  3. 2019南京区块链论坛小结
  4. 【力扣刷题——字符串】附力扣链接、题目描述、解题方法及代码(344、541、剑指Offer05、151、剑指 Offer58、28、459)后续再补充
  5. Bootstrap JavaScript插件:轮播插件 (carousel.js)
  6. 数字媒体技术专业延伸方向——特效
  7. [Python Scrapy爬虫] 二.翻页爬取农产品信息并保存本地
  8. R语言使用ggplot绘制线型与点
  9. 公众号接口消息推送+VB+WebService实战
  10. 智能手表音频特性测试_中学生可以戴99.9元的小米有品-Haylou/嘿喽智能手表上学吗?...