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

  • 相关阅读:
    进程实际操作篇2
    进程的实际操作篇1
    进程的理论知识
    解决套接字粘包,udp套接字对象的使用和socketserver模块实现并发
    day24-网络知识扫盲,socket的基本使用
    day23-网络编程之互联网基础,tcp/ip协议详细介绍
    day21-多态和多态性,鸭子类型,反射,内置方法,异常处理
    JAVA WEB小测
    JAVA动手动脑
    JAVA课上动手动脑问题2
  • 原文地址:https://www.cnblogs.com/chenxizhang/p/1649043.html
Copyright © 2011-2022 走看看