zoukankan      html  css  js  c++  java
  • WPF:逻辑树和视觉树

    通过下面的方式可以查看WPF的控件树,分为两种:逻辑树和视觉树。其中逻辑树是视觉树的子集。
            public Window1()
            {
                InitializeComponent();
    
    
    
                Debug.WriteLine("逻辑树");
                PrintLogicalTree(0, this);
    
    
            }
    
            protected override void OnContentRendered(EventArgs e)
            {
                base.OnContentRendered(e);
                Debug.WriteLine("视觉树");
    
                PrintVisualTree(0, this);
            }
    
            void PrintLogicalTree(int depth, object obj)
            {
                Debug.WriteLine(new string(' ', depth*2) + obj);
                if (!(obj is DependencyObject)) return;
                foreach (object child in LogicalTreeHelper.GetChildren(obj as DependencyObject))
                    PrintLogicalTree(depth + 1, child);
            }
            void PrintVisualTree(int depth, DependencyObject obj)
            {
                Debug.WriteLine(new string(' ', depth*2) + obj);
                for (int i = 0; i < VisualTreeHelper.GetChildrenCount(obj); i++)
                    PrintVisualTree(depth + 1, VisualTreeHelper.GetChild(obj, i));
            }

    运行结果如下

    逻辑树
    WpfApplication1.Window1
      System.Windows.Controls.Grid
        System.Windows.Controls.TextBox
        System.Windows.Controls.Label
        System.Windows.Controls.Button: Button
          Button
        System.Windows.Controls.Button: Button
          Button
        WpfApplication1.UserControl1
          System.Windows.Controls.Grid
            System.Windows.Controls.TextBox: Hello,World
              Hello,World


    视觉树
    WpfApplication1.Window1
      System.Windows.Controls.Border
        System.Windows.Documents.AdornerDecorator
          System.Windows.Controls.ContentPresenter
            System.Windows.Controls.Grid
              System.Windows.Controls.TextBox
                Microsoft.Windows.Themes.ListBoxChrome
                  System.Windows.Controls.ScrollViewer
                    System.Windows.Controls.Grid
                      System.Windows.Shapes.Rectangle
                      System.Windows.Controls.ScrollContentPresenter
                        System.Windows.Controls.TextBoxView
                          System.Windows.Media.DrawingVisual
                        System.Windows.Documents.AdornerLayer
                      System.Windows.Controls.Primitives.ScrollBar 最小值:0 最大值:0 值:0
                      System.Windows.Controls.Primitives.ScrollBar 最小值:0 最大值:0 值:0
              System.Windows.Controls.Label
                System.Windows.Controls.Border
                  System.Windows.Controls.ContentPresenter
                    System.Windows.Controls.TextBlock
              System.Windows.Controls.Button: Button
                Microsoft.Windows.Themes.ButtonChrome
                  System.Windows.Controls.ContentPresenter
                    System.Windows.Controls.TextBlock
              System.Windows.Controls.Button: Button
                Microsoft.Windows.Themes.ButtonChrome
                  System.Windows.Controls.ContentPresenter
                    System.Windows.Controls.TextBlock
              WpfApplication1.UserControl1
                System.Windows.Controls.Border
                  System.Windows.Controls.ContentPresenter
                    System.Windows.Controls.Grid
                      System.Windows.Controls.TextBox: Hello,World
                        Microsoft.Windows.Themes.ListBoxChrome
                          System.Windows.Controls.ScrollViewer
                            System.Windows.Controls.Grid
                              System.Windows.Shapes.Rectangle
                              System.Windows.Controls.ScrollContentPresenter
                                System.Windows.Controls.TextBoxView
                                  System.Windows.Media.DrawingVisual
                                System.Windows.Documents.AdornerLayer
                              System.Windows.Controls.Primitives.ScrollBar 最小值:0 最大值:0 值:0
                              System.Windows.Controls.Primitives.ScrollBar 最小值:0 最大值:0 值:0
          System.Windows.Documents.AdornerLayer

  • 相关阅读:
    教你如何剖析源码
    singleCall单来源调用解析及实现
    守护进程详细解读
    终端&作业控制&会话启动过程
    时间复杂度&空间复杂度
    linux环形buff模拟多线程信号量操作
    linux多线程-互斥&条件变量与同步
    linux线程控制&线程分离
    栈帧的不安全程序示例
    如何获取程序返回值,退出码,错误码
  • 原文地址:https://www.cnblogs.com/chenxizhang/p/1649043.html
Copyright © 2011-2022 走看看