public static class VisualTreeExtensions
{
/// <summary>
/// 获取父节点控件
/// </summary>
/// <typeparam name="T">子控件UI</typeparam>
/// <param name="obj">子控件</param>
/// <param name="name">父容器名称</param>
/// <returns>父容器对象</returns>
public static T GetParent<T>(this DependencyObject obj, string name = null) where T : FrameworkElement
{
DependencyObject parent = VisualTreeHelper.GetParent(obj); while (parent != null)
{
if (parent is T && (((T)parent).Name == name || string.IsNullOrEmpty(name)))
{
return (T)parent;
}
parent = VisualTreeHelper.GetParent(parent);
} return null;
}
/// <summary>
/// 获取子控件
/// </summary>
/// <typeparam name="T">UI类型</typeparam>
/// <param name="obj">父容器对象</param>
/// <param name="name">子控件名称</param>
/// <returns>子控件</returns>
public static T GetChild<T>(this DependencyObject obj, string name = null) where T : FrameworkElement
{
DependencyObject child = null;
T grandChild = null; for (int i = ; i <= VisualTreeHelper.GetChildrenCount(obj) - ; i++)
{
child = VisualTreeHelper.GetChild(obj, i); if (child is T && (((T)child).Name == name | string.IsNullOrEmpty(name)))
{
return (T)child;
}
else
{
grandChild = GetChild<T>(child, name);
if (grandChild != null)
return grandChild;
}
}
return null;
}
/// <summary>
/// 获取子控件集合
/// </summary>
/// <typeparam name="T">UI类型</typeparam>
/// <param name="obj">父容器对象</param>
/// <param name="name">子控件名称</param>
/// <returns>子控件集合</returns>
public static List<T> GetChildren<T>(this DependencyObject obj, string name = null) where T : FrameworkElement
{
DependencyObject child = null;
List<T> childList = new List<T>(); for (int i = ; i <= VisualTreeHelper.GetChildrenCount(obj) - ; i++)
{
child = VisualTreeHelper.GetChild(obj, i);
if (child is T && (((T)child).Name == name || string.IsNullOrEmpty(name)))
{
childList.Add((T)child);
}
childList.AddRange(GetChildren<T>(child, ""));
}
return childList;
} /// <summary>
/// 查找某控件下的所有給定類型的子控件集合
/// </summary>
/// <typeparam name="T">要查找的子控件類型</typeparam>
/// <param name="depObj">父控件</param>
/// <returns>子控件集合</returns>
public static T FindVisualChild<T>(this DependencyObject depObj, string childName = null) where T : FrameworkElement
{
if (depObj != null)
{
for (int i = ; i < VisualTreeHelper.GetChildrenCount(depObj); i++)
{
DependencyObject child = VisualTreeHelper.GetChild(depObj, i);
if (child != null && child is T &&
(((T)child).Name == childName || string.IsNullOrEmpty(childName)))
{
return (T)child;
}
else
{
T childOfChild = FindVisualChild<T>(child, childName);
if (childOfChild != null && childOfChild is T)
{
return childOfChild;
}
}
}
return null;
}
return null;
} /// <summary>
/// 查找某控件下的所有給定類型的子控件集合
/// </summary>
/// <typeparam name="T">要查找的子控件類型</typeparam>
/// <param name="depObj">父控件</param>
/// <returns>子控件集合</returns>
public static List<T> FindVisualChildren<T>(this DependencyObject depObj, string childName = null) where T : FrameworkElement
{
List<T> list = new List<T>();
if (depObj != null)
{
for (int i = ; i < VisualTreeHelper.GetChildrenCount(depObj); i++)
{
DependencyObject child = VisualTreeHelper.GetChild(depObj, i);
if (child != null && child is T
&& (((T)child).Name == childName || string.IsNullOrEmpty(childName)))
{
list.Add((T)child);
} List<T> childItems = FindVisualChildren<T>(child, childName);
if (childItems != null && childItems.Count > )
{
foreach (var item in childItems)
{
list.Add(item);
}
}
}
}
return list;
}
}

WPF silverlight获取子控件(获取DataTemplate里的子控件)的更多相关文章

  1. WPF中获取TreeView以及ListView获取其本身滚动条的方法,可实现自行调节scoll滚动的位置(可相应获取任何控件中的内部滚动条)

    原文:WPF中获取TreeView以及ListView获取其本身滚动条的方法,可实现自行调节scoll滚动的位置(可相应获取任何控件中的内部滚动条) 对于TreeView而言: TreeViewAut ...

  2. WPF设置控件获取键盘焦点时的样式FocusVisualStyle

    控件获取焦点除了用鼠标外,可以通过键盘来获取,比如Tab键或者方向键等,需要设置控件获取键盘焦点时的样式,可以通过设置FrameworkElemnt.FocusVisualStyle属性, 因为几乎所 ...

  3. 初步探讨WPF的ListView控件(涉及模板、查找子控件) - GavinJun

    本文结合模板的应用初步介绍ListView的应用 一.Xaml中如何建立数据资源 大部分数据都会来自于后台代码,如何Xaml同样的建立数据源呢?比如建立一个学生List: 首先引入命名空间: xmln ...

  4. 初步探讨WPF的ListView控件(涉及模板、查找子控件)

    本文结合模板的应用初步介绍ListView的应用 一.Xaml中如何建立数据资源 大部分数据都会来自于后台代码,如何Xaml同样的建立数据源呢?比如建立一个学生List: 首先引入命名空间: xmln ...

  5. CheckBoxList控件获取多选择,需要遍历

    CheckBoxList控件获取多选择,需要遍历,环境:vs2008 在页面上添加CheckBoxList控件,输入项值 a,b,c,d.然后添加按钮 Button2确定,如何获取CheckBoxLi ...

  6. JavaScript经典代码【一】【javascript HTML控件获取值】

    javascript HTML控件获取值 1.下拉列表框选定值 ddlPageSize.options[ddlPageSize.selectedIndex].value ddlPageSize.opt ...

  7. 通过控件获取cell

    #pragma mark - 通过控件获取cell -(UITableViewCell*)GetCellFromTableView:(UITableView*)tableView Sender:(id ...

  8. JS 获取某个容器控件中id包含制定字符串的控件id列表

    //获取某容器控件中id包含某字符串的控件id列表 //参数:容器控件.要查找的控件的id关键字 function GetIdListBySubKey(container,subIdKey) { va ...

  9. Element ui tree树形控件获取当前节点id和父节点id

    低版本Element ui tree树形控件获取当前节点id和父节点id的方法:点击查看 最新版本Element ui tree树形控件获取当前节点id和父节点id教程: 1.找到node_modul ...

随机推荐

  1. Project Euler:Problem 88 Product-sum numbers

    A natural number, N, that can be written as the sum and product of a given set of at least two natur ...

  2. Http multipart/form-data多参数Post方式上传数据

    最近,工作中遇到需要使用java实现http发送get.post请求,简单的之前经常用到,但是这次遇到了上传文件的情况,之前也没深入了解过上传文件的实现,这次才知道通过post接口也可以,是否还有其他 ...

  3. Java做一个时间的程序,为什么要除以1000*60*60*24啊。这个数字是什么意思啊。

    1000耗秒(1秒),60秒(1分),60分(1小时),24小时(1天)

  4. 初涉springboot

    1.首先,我们需要了解微服务是什么? 微服务 (Microservices) 是一种软件架构风格,它是以专注于单一责任与功能的小型功能区块 (Small Building Blocks) 为基础,利用 ...

  5. mybatis、spring、mysql、maven实现简单增删查改

    之前写过的mybatis博客作为学习mybatis.spring还是不太合适. 现在找到一个不错的例子,首先将这个完整的mybatis增删查改例子在本地上实现出来,然后再进行学习. 项目结构与运行结果 ...

  6. POJ 2665 模拟,,

    It is confirmed that these sections do not overlap with each other. 一句话 就变成水题了,,, // by SiriusRen #i ...

  7. CaffeNet用于Flickr Style数据集上的风格识别

    转自 http://blog.csdn.net/liumaolincycle/article/details/48501423 微调是基于已经学习好的模型的,通过修改结构,从已学习好的模型权重中继续训 ...

  8. tp5数据库操作 Db类

    一.链接数据库 1.配置文件定义  application\database.php 注意:数据表前缀更改,在文件的prefix选项 2.类定义 二.数据库的基本使用 namespace app\de ...

  9. 【Paper Reading】Learning while Reading

    Learning while Reading 不限于具体的书,只限于知识的宽度 这个系列集合了一周所学所看的精华,它们往往来自不只一本书 我们之所以将自然界分类,组织成各种概念,并按其分类,主要是因为 ...

  10. 面向对象和结构化程序设计的区别X

    面向对象和结构化程序设计的区别 结构化程序的概念首先是从以往编程过程中无限制地使用转移语句而提出的.转移语句可以使程序的控制流程强制性的转向程序的任一处,在传统流程图中,就是用上节我们提到的" ...