码迷,mamicode.com
首页 > 其他好文 > 详细

视觉树

时间:2015-03-20 10:46:28      阅读:110      评论:0      收藏:0      [点我收藏+]

标签:

 

 1       /// <summary>
 2         /// 获取指定类型的可视父对象
 3         /// </summary>
 4         /// <typeparam name="T">指定类型</typeparam>
 5         /// <param name="obj">传入的对象</param>
 6         /// <returns></returns>
 7         public static T GetParentObject<T>(DependencyObject obj) where T : FrameworkElement
 8         {
 9             DependencyObject parent = VisualTreeHelper.GetParent(obj);
10 
11             while (parent != null)
12             {
13                 if (parent is T)
14                 {
15                     return (T)parent;
16                 }
17                 parent = VisualTreeHelper.GetParent(parent);
18             }
19             return null;
20         }

 

 

 1      /// <summary>
 2         /// 获取指定类型的可视子对象
 3         /// </summary>
 4         /// <typeparam name="T">指定类型</typeparam>
 5         /// <param name="obj">传入的对象</param>
 6         /// <returns></returns>
 7         public static T GetChildObject<T>(DependencyObject obj) where T : FrameworkElement
 8         {
 9             DependencyObject child = null;
10             T grandChild = null;
11 
12             for (int i = 0; i <= VisualTreeHelper.GetChildrenCount(obj) - 1; i++)
13             {
14                 child = VisualTreeHelper.GetChild(obj, i);
15 
16                 if (child is T)
17                 {
18                     return (T)child;
19                 }
20                 else
21                 {
22                     grandChild = GetChildObject<T>(child);
23                     if (grandChild != null)
24                         return grandChild;
25                 }
26             }
27             return null;
28         }

 

视觉树

标签:

原文地址:http://www.cnblogs.com/XzcBlog/p/4352937.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!