代码语言
.
CSharp
.
JS
Java
Asp.Net
C
MSSQL
PHP
Css
PLSQL
Python
Shell
EBS
ASP
Perl
ObjC
VB.Net
VBS
MYSQL
GO
Delphi
AS
DB2
Domino
Rails
ActionScript
Scala
代码分类
文件
系统
字符串
数据库
网络相关
图形/GUI
多媒体
算法
游戏
Jquery
Extjs
Android
HTML5
菜单
网页交互
WinForm
控件
企业应用
安全与加密
脚本/批处理
开放平台
其它
【
】
WPF 页面控件找到子孙控件
作者:
dezai
/ 发布于
2014/3/11
/
637
WPF 页面控件找到子孙控件
using System; using System.Collections.Generic; using System.Linq; using System.Windows; using System.Windows.Media; using System.Windows.Media.Media3D; namespace Gishu.WPF.Utilities { static class DependencyObjExtensions { /// <summary> /// Find the first child of the specified type (the child must exist) /// by walking down the logical/visual trees /// Will throw an exception if a matching child does not exist. If you're not sure, use the TryFindChild method instead. /// </summary> /// <typeparam name="T">The type of child you want to find</typeparam> /// <param name="parent">The dependency object whose children you wish to scan</param> /// <returns>The first descendant of the specified type</returns> /// <remarks> usage: myWindow.FindChild<StackPanel>() </StackPanel></remarks> public static T FindChild<T>(this DependencyObject parent) where T : DependencyObject { return parent.FindChild<T>(child => true); } /// <summary> /// Find the first child of the specified type (the child must exist) /// by walking down the logical/visual trees, which meets the specified criteria /// Will throw an exception if a matching child does not exist. If you're not sure, use the TryFindChild method instead. /// </summary> /// <typeparam name="T">The type of child you want to find</typeparam> /// <param name="parent">The dependency object whose children you wish to scan</param> /// <param name="predicate">The child object is selected if the predicate evaluates to true</param> /// <returns>The first matching descendant of the specified type</returns> /// <remarks> usage: myWindow.FindChild<StackPanel>( child => child.Name == "myPanel" ) </StackPanel></remarks> public static T FindChild<T>(this DependencyObject parent, Func<T, bool> predicate) where T : DependencyObject { return parent.FindChildren<T>(predicate).First(); } /// <summary> /// Use this overload if the child you're looking may not exist. /// </summary> /// <typeparam name="T">The type of child you're looking for</typeparam> /// <param name="parent">The dependency object whose children you wish to scan</param> /// <param name="foundChild">out param - the found child dependencyobject, null if the method returns false</param> /// <returns>True if a child was found, else false</returns> public static bool TryFindChild<T>(this DependencyObject parent, out T foundChild) where T : DependencyObject { return parent.TryFindChild<T>(child => true, out foundChild); } /// <summary> /// Use this overload if the child you're looking may not exist. /// </summary> /// <typeparam name="T">The type of child you're looking for</typeparam> /// <param name="parent">The dependency object whose children you wish to scan</param> /// <param name="predicate">The child object is selected if the predicate evaluates to true</param> /// <param name="foundChild">out param - the found child dependencyobject, null if the method returns false</param> /// <returns>True if a child was found, else false</returns> public static bool TryFindChild<T>(this DependencyObject parent, Func<T, bool> predicate, out T foundChild) where T : DependencyObject { foundChild = null; var results = parent.FindChildren<T>(predicate); if (results.Count() == 0) return false; foundChild = results.First(); return true; } /// <summary> /// Get a list of descendant dependencyobjects of the specified type and which meet the criteria /// as specified by the predicate /// </summary> /// <typeparam name="T">The type of child you want to find</typeparam> /// <param name="parent">The dependency object whose children you wish to scan</param> /// <param name="predicate">The child object is selected if the predicate evaluates to true</param> /// <returns>The first matching descendant of the specified type</returns> /// <remarks> usage: myWindow.FindChildren<StackPanel>( child => child.Name == "myPanel" ) </StackPanel></remarks> public static IEnumerable<T> FindChildren<T>(this DependencyObject parent, Func<T, bool> predicate) where T : DependencyObject { var children = newList<DependencyObject>(); if ((parent is Visual) || (parent is Visual3D)) { var visualChildrenCount = VisualTreeHelper.GetChildrenCount(parent); for (int childIndex = 0; childIndex < visualChildrenCount; childIndex++) children.Add(VisualTreeHelper.GetChild(parent, childIndex)); } foreach (var logicalChild in LogicalTreeHelper.GetChildren(parent).OfType<DependencyObject>()) if (!children.Contains(logicalChild)) children.Add(logicalChild); foreach (var child in children) { var typedChild = child as T; if ((typedChild != null) && predicate.Invoke(typedChild)) yield return typedChild; foreach (var foundDescendant in FindChildren(child, predicate)) yield return foundDescendant; } yield break; } } }
试试其它关键字
子孙控件
同语言下
.
StringHelper.cs 对html标签过滤
.
不调用Dbhelper数据库的后台代码
.
后台弹出提示框,防止页面刷新。
.
倒计时间表
.
JAVA集成SVN,查看应用更新日志
.
输入一串无序数,返回出现次数最多的数字,并返回个数
.
table中连续字符换行
.
WPF 获取屏幕分辨率
.
简单的实现用户注册时,向其油箱发送激活码邮件,并进
.
JavaMail发送邮件
可能有用的
.
C#实现的html内容截取
.
List 切割成几份 工具类
.
SQL查询 多列合并成一行用逗号隔开
.
一行一行读取txt的内容
.
C#动态修改文件夹名称(FSO实现,不移动文件)
.
c# 移动文件或文件夹
.
c#图片添加水印
.
Java PDF转换成图片并输出给前台展示
.
网站后台修改图片尺寸代码
.
处理大图片在缩略图时的展示
dezai
贡献的其它代码
(
1065
)
.
双色球
.
列出所有物理网络适配器
.
快乐数的 Python 实现
.
计算当月还剩天数
.
猜属相
.
二十四小时时钟
.
每日一语
.
很酷的日历
.
超长日历表单
.
最简单的时钟
Copyright © 2004 - 2024 dezai.cn. All Rights Reserved
站长博客
粤ICP备13059550号-3