WPF中的VisualTreeHelper

VisualTreeHelper提供了一组WPF控件树模型,通过VisualTreeHelper可以遍历控件的树形结构,获取我们所需要的控件以及相应的属性;

VisualTreeHelper提供了一下4个方法:

1、FindElementsInHostCoordinates 检索一组对象,这些对象位于某一对象的坐标空间的指定点或 Rect 内。
2、GetChild 使用提供的索引,通过检查可视化树获取所提供对象的特定子对象。
3、GetChildrenCount 返回在可视化树中在某一对象的子集合中存在的子级的数目。
4、GetParent 返回可视化树中某一对象的父对象。

通俗点说:FindElementsInHostCoordinates常用于对象的碰撞检测,GetChild用于获取下级子对象(注意仅仅是下级,而非所有子对象,如果要获取所有子对象,需要自己写代码遍历),GetChildrenCount用于获取下级子对象的个数,GetParent用于获取某对象的上级子对象;

下面是简单的检索子对象的常用方法:

 1 代码 
 2 using System.Linq;
 3 using System.Windows;
 4 using System.Collections.Generic;
 5 using System.Windows.Controls;
 6 using System.Windows.Media;
 7 
 8 
 9 namespace ToolsTest
10 {
11     public partial class MainPage : UserControl
12     {
13         public MainPage()
14         {
15             InitializeComponent();
16         }
17 
18         private void btnClick_Click(object sender, RoutedEventArgs e)
19         {
20             int _childCount = VisualTreeHelper.GetChildrenCount(this);
21             MessageBox.Show("MainPage下级子对象总数:" + _childCount.ToString());//就是一个Grid,所以返回1
22 
23             IEnumerable<Button> AllButtons = FindChildren<Button>(this);//得到所有的Buttons
24 
25             int i =0;
26             foreach (Button btn in AllButtons)
27             {
28                 i++;
29                 MessageBox.Show(string.Format("第{0}个按钮[{1}]的内容为:{2}",i,btn.Name,btn.Content));                
30             }
31 
32             StackPanel sp = VisualTreeHelper.GetParent(btn2) as StackPanel;
33             MessageBox.Show(string.Format("{0}的上级对象是{1}",btn2.Content,sp.Name));
34 
35             Rect rect = new Rect(0, 0, 100, 25);
36 
37             IEnumerable<UIElement> check = VisualTreeHelper.FindElementsInHostCoordinates(rect, this); //检测MainPage的0,0到100,25矩形区域内有哪些元素
38 
39             foreach (UIElement item in check)
40             {
41                 string _name = item.GetValue(NameProperty).ToString();
42                 if (_name.Length > 0)
43                 {
44                     MessageBox.Show(_name);
45                 }
46             }           
47         }
48 
49 
50         /// <summary>
51         /// FindChildren
52         /// </summary>
53         /// <typeparam name="T"></typeparam>
54         /// <param name="parent"></param>
55         /// <returns></returns>
56         public IEnumerable<T> FindChildren<T>(DependencyObject parent) where T : class
57         {
58             var count = VisualTreeHelper.GetChildrenCount(parent);
59             if (count > 0)
60             {
61                 for (var i = 0; i < count; i++)
62                 {
63                     var child = VisualTreeHelper.GetChild(parent, i);
64                     var t = child as T;
65                     if (t != null)
66                         yield return t;
67 
68                     var children = FindChildren<T>(child);
69                     foreach (var item in children)
70                         yield return item;
71                 }
72             }
73         }
74     }
75 }

参考博客地址:
http://www.cnblogs.com/yjmyzz/archive/2010/01/02/1638006.html

原文地址:https://www.cnblogs.com/xibei666/p/4377337.html