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

posted @ 2010-01-16 11:03  陈希章  阅读(2466)  评论(0编辑  收藏  举报