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; 
  • 相关阅读:
    VC获取系统时间、程序运行时间
    数学题
    最小费用流
    最大流模板
    计划
    算法竞赛入门经典 训练指南 之 图论(完全版持续更新)
    uva 11324 The Largest Clique 强连通分量求缩点构造DAG
    hdu 4288 Coder 一个很水的版本 >_<
    hoj 2939 Coin Question
    成都网络赛 1002 Control 1005 Food
  • 原文地址:https://www.cnblogs.com/fx2008/p/2755125.html
Copyright © 2011-2022 走看看