zoukankan      html  css  js  c++  java
  • 【转载】wpf 查找指定类型的子控件

    http://blog.csdn.net/fallincloud/article/details/6960255#

    查找指定类型的子控件


    1. /// <summary> 
    2. /// Find Child with Visual Tree 
    3. /// </summary> 
    4. /// <typeparam name="T">specail type</typeparam> 
    5. /// <param name="root">the element starts</param> 
    6. /// <returns></returns> 
    7. public static T FindChild<T>(DependencyObject root) where T : DependencyObject 
    8.     if (root == null
    9.         return null
    10.  
    11.     T founded = null
    12.  
    13.     for (int j = 0; j < VisualTreeHelper.GetChildrenCount(root); j++) 
    14.     { 
    15.         DependencyObject d = VisualTreeHelper.GetChild(root, j); 
    16.         T childType = d as T; 
    17.         if (childType == null
    18.         { 
    19.             founded = FindChild<T>(d); 
    20.             if (founded != null
    21.                 break
    22.         } 
    23.         else 
    24.         { 
    25.             founded = childType; 
    26.             break
    27.         } 
    28.     } 
    29.  
    30.     return founded; 

    查找指定类型指定名称的子控件:

    1. /// <summary> 
    2. /// Find Child with Visual Tree 
    3. /// </summary> 
    4. /// <typeparam name="T">specail type</typeparam> 
    5. /// <param name="root">the element starts</param> 
    6. /// <returns></returns> 
    7. public static T FindChild<T>(DependencyObject root, string name) where T : DependencyObject 
    8.     if (root == null
    9.         return null
    10.  
    11.     T founded = null
    12.  
    13.     for (int j = 0; j < VisualTreeHelper.GetChildrenCount(root); j++) 
    14.     { 
    15.         DependencyObject d = VisualTreeHelper.GetChild(root, j); 
    16.         T childType = d as T; 
    17.         if (childType == null || ((d is FrameworkElement) && (d as FrameworkElement).Name != name)) 
    18.         { 
    19.             founded = FindChild<T>(d, name); 
    20.             if (founded != null
    21.                 break
    22.         } 
    23.         else 
    24.         { 
    25.             founded = childType; 
    26.             break
    27.         } 
    28.     } 
    29.  
    30.     return founded; 


    查找父控件:

    1. public static T FindParent<T>(DependencyObject d) where T : DependencyObject 
    2.     DependencyObject parent = d; 
    3.     while (parent != null
    4.     { 
    5.         parent = VisualTreeHelper.GetParent(parent); 
    6.         if (parent != null && (parent.GetType() == typeof(T))) 
    7.         { 
    8.             return parent as T; 
    9.         } 
    10.     } 
    11.     return parent as T; 
  • 相关阅读:
    「UOJ#117」 欧拉回路
    「LuoguP1341」 无序字母对(欧拉回路
    「NOIP2002」「Codevs1099」 字串变换(BFS
    「IOI1998」「LuoguP4342」Polygon(区间dp
    「LuoguP2420」 让我们异或吧(树上前缀和
    「USACO13MAR」「LuoguP3080」 牛跑The Cow Run (区间dp
    「LuoguP1220」 关路灯(区间dp
    「CQOI2007」「BZOJ1260」涂色paint (区间dp
    「LuoguP1430」 序列取数(区间dp
    「USACO16OPEN」「LuoguP3147」262144(区间dp
  • 原文地址:https://www.cnblogs.com/fx2008/p/2755125.html
Copyright © 2011-2022 走看看