首页 > 其他分享 >WPF LogicalTree vs Visual Tree

WPF LogicalTree vs Visual Tree

时间:2024-08-25 17:53:23浏览次数:11  
标签:Windows tree Tree System vs using WPF structure

Copy from https://www.c-sharpcorner.com/blogs/wpf-logical-and-visual-trees1

 

 

WPF's hierarchical structure requires a new conceptual model of application structure, which takes the form of an element tree. Two types of element trees are required to adequately represent an element structure: a Logical Tree and a Visual Tree.

LOGICAL TREE: The Logical Tree is a tree structure that represents the hierarchy of controls and elements that constitute a piece of user interface in WPF, but without their inner parts. The logical tree is responsible for:

1. Inherit DependencyProperty values
2. Resolving DynamicResources references
3. Looking up element names for bindings
4. Forwaring RoutedEvents

Example:

DockPanel
 +ListBox
   +Button
   +Button
   +Button

VISUAL TREE: The Visual Tree is a tree structure that represents the fine-grained hierarchy of Visuals that constitute what appears on the screen. It contains the same elements as the Logical Tree, but includes all the Visuals that are used to compose the Logical Tree's controls and elements, like the ControlTemplates and DataTemplates. The visual tree is responsible for:

1. Rendering visual elements
2. Propagate element opacity
3. Propagate Layout- and RenderTransforms
4. Propagate the IsEnabled property.
5. Do Hit-Testing
6. RelativeSource 

Example:

DockPanel
    +ListBox
      +Border
        +...
        +VirtualizingStackPanel
          +ListBoxItem
            +Button
              +Border
              +....

 

 

 

using System;
using System.Collections.Generic;
using System.Diagnostics;
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 WpfApp283
{
    /// <summary>
    /// Interaction logic for MainWindow.xaml
    /// </summary>
    public partial class MainWindow : Window
    {
        public MainWindow()
        {
            InitializeComponent();
            PrintLogicalTree(0, this);
            //var logicalTrees=LogicalTreeHelper.GetChildren(this);
        }

        private void PrintLogicalTree(int depth, object obj)
        {
            Debug.WriteLine($"LogicalTree:{depth}");
            if (!(obj is DependencyObject))
            {
                return;
            }

            foreach (object child in LogicalTreeHelper.GetChildren(obj as DependencyObject))
            {
                PrintLogicalTree(depth+1, child);
            }
        }

        protected override void OnContentRendered(EventArgs e)
        {
            base.OnContentRendered(e);
            PrintVisualTree(0, this);
        }

        void PrintVisualTree(int depth,DependencyObject obj)
        {
            Debug.WriteLine($"Visualtree:{depth}");
            for(int i=0;i<VisualTreeHelper.GetChildrenCount(obj);i++)
            {
                PrintVisualTree(depth+1,VisualTreeHelper.GetChild(obj,i));
            }
        }
    }
}

 

标签:Windows,tree,Tree,System,vs,using,WPF,structure
From: https://www.cnblogs.com/Fred1987/p/18379236

相关文章

  • WPF CanFreeze Freeze IsFrozen
    Freezable—Thebaseclassforobjectsthatcanbe“frozen”intoaread-onlystatefor performancereasons.Freezables,oncefrozen,canbesafelysharedamongmultiple threads,unlikeallotherDispatcherObjects.Frozenobjectscanneverbeunfrozen,buty......
  • x64汇编第一讲,Vs系列配置x64环境与x86环境
    x64汇编第一讲,Vs系列配置x64环境与x86环境IBinary关注发布于2019-05-2516:07:403K0发布于2019-05-2516:07:40举报文章被收录于专栏:逆向技术目录x64汇编环境配置一丶x64环境配置1.1VS系列编译器配置X64Asm开发环境.二丶Vs配置X......
  • Minimum Steiner Tree 题解
    原题,详见P10723。几乎相同,我们只需要以一个需要选择的点为根,遍历子树看看有没有出现需要选择的点,然后直接去删除即可,可以看我的博客。但是我们也可以换一种做法,用类似拓扑排序的算法。先找到所有只连一条边且没有被选择的点,然后放进队列,接着依次取出队头遍历与它相连的点,同时记......
  • 树上启发式合并——dsu on tree
    参考文章:树上启发式合并[dsuontree]树上启发式合并总结树上启发式合并の详解启发式合并启发式算法是什么呢?启发式算法是基于人类的经验和直观感觉,对一些算法的优化。举个例子,最常见的就是并查集的启发式合并了,代码是这样的:voidmerge(intx,inty){intxx=find(x......
  • LVS
    问题LVSNAT模式必须设置默认路由指向DIR地址,如果不设置数据包RS服务器能否将数据包正确的返回给客户端。DR/TUN模式增加一条静态路由目标地址VIP地址,dev设备指向VIP的网口,如果不设置RS服务能否正确接收到DirectorServer发送的数据包LVSDR/TUN模式,RS服务器一定配置VIP地址在......
  • 嵌入式UI开发-lvgl+wsl2+vscode系列:12、GUI Guider安装使用及在ssd202开发板上测试
    一、前言接下来我们根据开发板官方的指南安装lvgl的ui工具GUIGuider进行开发和测试。理论上还有SquareLineStudio,但是由于一些收费等因素暂时不做过多介绍,gui工具只是辅助,加快开发效率,很多时候还是得直接用代码写界面。(还有一个原因就是GUIGuider可以直接设置中文界面)......
  • ABC 368D Minimum Steiner Tree
    题意给你一颗由N个点组成的树,指定K个节点,求包含这K个节点的最小子树的大小思路考虑正难则反,我们从开始的树当中剪掉那些没有任何指定点的子树,剩下来的子树就是最小的、能包含所有指定节点的子树。关于剪去这个操作,就是dfs一旦遇到以当前节点为根的子树没有任何指定点时,就停止df......
  • Ros2 vscode 调试运行
    1.编译调试版本:在使用colconbuild编译工作区时,你需要确保以调试模式编译,添加--cmake-args-DCMAKE_BUILD_TYPE=Debug参数:colconbuild--cmake-args-DCMAKE_BUILD_TYPE=Debug2.使用gdb进行调试gdb是一个常用的调试工具,可以帮助你在命令行中调试ROS2节点。步......
  • VS2022 Visual Studio Installer 一直卡在0%,或者下载速度慢的问题解决办法
    C:\Users\Administrator\AppData\Local\Temp到c盘查看日志,发现是下载一个叫vs_installer.opc的东西失败了, 直接复制日志里的https://aka.ms/vs/17/release/installer,下载,发现成功下载,然后放到installer安装器同级目录,重新打开setup安装,就成功了打开了,然后会一直正在准备中,......
  • 十五、OpenCVSharp实现相机标定
    文章目录简介一、相机模型1.针孔相机模型2.畸变模型(径向畸变、切向畸变)二、标定板的设计和使用1.常见的标定板类型(如棋盘格、圆形标定板)2.标定板图像的采集要求三、相机标定的步骤1.角点检测和提取2.求解相机内参和外参3.标定结果的评估和优......