WPF 中的树
逻辑树LogicalTree
逻辑树是在WPF框架级别定义的,这意味着与逻辑树操作最相关的WPF基本元素是FrameworkElement或FrameworkContentElement。
LogicalTreeHelper类
Object->LogicalTreeHelper
提供用于查询逻辑树中的对象的静态帮助器方法。
LogicalTreeHelper 对于分析方案非常有用,在这种情况下,将以递归方式遍历逻辑树,并使用一致的方法检查各种父对象或子对象
名称 | 备注 | 权限 |
---|---|---|
尝试使所请求的 UI 元素可见,并在目标上引发 RequestBringIntoView 事件以报告结果。 | public static | |
尝试查找并返回具有指定名称的对象。 搜索从指定对象开始,并持续到逻辑树的子节点中。 | public static | |
通过处理逻辑树,返回指定对象的直接子对象的集合。 | public static | |
通过处理逻辑树,返回指定对象的父对象。 | public static |
可视化树 VisualTree
WPF 中除了逻辑树的概念,还存在可视化树的概念。 可视化树描述了可视化对象的结构。
VisualTreeHelper类
Object->VisualTreeHelper
提供一些实用工具方法,用于执行涉及可视化树中的节点的常规任务。
名称 | 备注 | 权限 |
---|---|---|
返回指定 BitmapEffect 的 Visual 值。 | public static | |
返回指定 BitmapEffectInput 的 Visual 值。 | public static | |
检索指定的 Visual 的缓存表示形式。 | public static | |
返回指定父可视对象中位于指定集合索引位置的子可视对象。 | public static | |
返回指定可视对象包含的子级个数。 | public static | |
将指定的 Visual 的剪辑区域作为 Geometry 值返回。 | public static | |
返回 Visual 的已缓存的边界框矩形。 | public static | |
返回视觉对象所有后代的全部内容范围框的联合,其中包括 Visual 的内容范围框。 | public static | |
获取测量和呈现此视觉对象的 DPI 信息。 | public static | |
返回指定的 Visual 的绘图内容。 | public static | |
将指定的 Visual 的边缘模式作为 EdgeMode 值返回。 | public static | |
获取指定 Visual 的位图效果。 | public static | |
返回 Visual 的偏移量。 | public static | |
返回 Visual 的不透明度。 | public static | |
返回一个 Brush 值,该值表示 Visual 的不透明蒙板。 | public static | |
返回表示视觉对象的父对象的 DependencyObject 值。 | public static | |
返回 Transform 的 Visual 值。 | public static | |
返回 X 坐标(垂直)准线集合。 | public static | |
返回 Y 坐标(水平)准线集合。 | public static | |
返回命中测试的最顶层 Visual 对象。 | public static | |
更新视觉对象的 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);
}
}
}
}