博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
【转载】wpf 查找指定类型的子控件
阅读量:6706 次
发布时间:2019-06-25

本文共 3014 字,大约阅读时间需要 10 分钟。

#

查找指定类型的子控件

[csharp]
  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; 
/// 		/// Find Child with Visual Tree		/// 		/// 
specail type
/// the element starts ///
public static T FindChild
(DependencyObject root) where T : DependencyObject { if (root == null) return null; T founded = null; for (int j = 0; j < VisualTreeHelper.GetChildrenCount(root); j++) { DependencyObject d = VisualTreeHelper.GetChild(root, j); T childType = d as T; if (childType == null) { founded = FindChild
(d); if (founded != null) break; } else { founded = childType; break; } } return founded; }

 

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

[csharp]
  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; 
/// 		/// Find Child with Visual Tree		/// 		/// 
specail type
/// the element starts ///
public static T FindChild
(DependencyObject root, string name) where T : DependencyObject { if (root == null) return null; T founded = null; for (int j = 0; j < VisualTreeHelper.GetChildrenCount(root); j++) { DependencyObject d = VisualTreeHelper.GetChild(root, j); T childType = d as T; if (childType == null || ((d is FrameworkElement) && (d as FrameworkElement).Name != name)) { founded = FindChild
(d, name); if (founded != null) break; } else { founded = childType; break; } } return founded; }

 

查找父控件:

[csharp]
  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; 

转载于:https://www.cnblogs.com/fx2008/archive/2012/11/05/2755125.html

你可能感兴趣的文章
Linux基础命令之echo(涉及bash命令引用及替换部分内容)
查看>>
exchange 2010英文版的安装前骤及步骤
查看>>
【iOS-Cocos2d游戏开发之五】【2】多触点与触屏事件详解(单一监听、事件分发)...
查看>>
青年菜君与小农女送菜商业模式PK
查看>>
ripv2&ospf路由重发布
查看>>
老虎机的制作
查看>>
Classloader和线程
查看>>
浅谈Attribute [C# | Attribute | DefaultValueAttribute]
查看>>
citus对join的支持
查看>>
【Cocos2d-X】iOS6 中 libcurl.a及iOS6中无法横屏的解决方法
查看>>
.NET开发常用知识点总结汇总
查看>>
源码配置bind主从时的注意事项
查看>>
英语每日听写练习 Day 6
查看>>
数组逆序重放(链表头插法练习)
查看>>
windows server 2008 安装实录
查看>>
安装卸载图形界面
查看>>
修改EXCHANGE默认的收发邮件大小是10M
查看>>
软raid的详细配置讲解 raid 0
查看>>
large-scale analysis of malware downloaders
查看>>
一道中级运维的shell面试题
查看>>