【C#】wpf查找父子节点
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; using System.Windows; using System.Windows.Media; namespace TestSeagate.Common { class UIAssociate { /// <summary> /// 查找第一个父节点是T类型的节点,并返回。 /// </summary> /// <typeparam name="T"></typeparam> /// <param name="dobj"></param> /// <returns></returns> public static T GetAncestor<T>(DependencyObject dobj) where T : FrameworkElement { DependencyObject parent = VisualTreeHelper.GetParent(dobj);//可视树 深入内部 while (!(parent is T) && parent != null) { parent = VisualTreeHelper.GetParent(parent); } if (parent != null) { return (T)parent; } else { return null; } } /// <summary> /// 查找所有满足条件的子节点, /// </summary> /// <typeparam name="T"></typeparam> /// <param name="depen"></param> /// <param name="childname"></param> /// <returns></returns> public static List<T> GetChildren<T>(DependencyObject depen,string childname=null) where T:FrameworkElement { DependencyObject child = null; List<T> lists = new List<T>(); for(int i=0;i<VisualTreeHelper.GetChildrenCount(depen);i++) { child = VisualTreeHelper.GetChild(depen,i); if((child is T)&&(((T)child).Name==childname||string.IsNullOrEmpty(childname))) { lists.Add((T)child); } lists.AddRange(GetChildren<T>(child, childname)); } return lists; } } }
低调大师中文资讯倾力打造互联网数据资讯、行业资源、电子商务、移动互联网、网络营销平台。
持续更新报道IT业界、互联网、市场资讯、驱动更新,是最及时权威的产业资讯及硬件资讯报道平台。
转载内容版权归作者及来源网站所有,本站原创内容转载请注明来源。
- 上一篇
【C#】wpf中的xmlns命名空间为什么是一个网址,代表了什么意思
原文: 【C#】wpf中的xmlns命名空间为什么是一个网址,代表了什么意思 新建一个wpf的项目,我们先来看下它默认的命名空间都是哪些? 可以看到xmlns有的是网址,有的是clr-namespace开头的一串字母。clr开头的比较好理解,就是运行时的命名空间,就像C#代码中的using xxxx之类的一样。 但是对于网址开头的命名空间有可能就有点疑惑了,它究竟代表的是什么?xaml文件被解析的时候会访问这个网址吗?如果这个网址那天不能get了,那我们的程序是不是就不能正常运行了。 在这里我先把结论告诉大家,然后在一步步分析是为什么。 结论: 以xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation这个为例,其实它就是System.Windows,System.Windows.Automation,System.Winjdows.Controls....等一系列命名空间的集合,是这个集合的【别名】,在浏览器输入这个网址有时候是不可访问的。如果自己定义类库的话,我把这个【别名】叫做张三也是可以的。微软建议,这个一...
- 下一篇
【C#】获取"我的电脑"的名字,如This PC、这台计算机
原文: 【C#】获取"我的电脑"的名字,如This PC、这台计算机 注意:这里获取的【我的电脑】的名字,不是机器的名字。如图所示: 要获取的是This PC这个字符串。 ------------------------------------------------------------------- 机器的名字可以通过系统属性查看,叫做Computer Name,如下图: 我的就是cn-c-w-725,如果想获取这个字符串直接调用Environment.MachineName就行了。 1.定义ShellAPI类 public static class ShellAPI { public const int MAX_PATH = 260; public const uint CMD_FIRST = 1; public const uint CMD_LAST = 30000; public const int S_OK = 0, S_FALSE = 1; public const int DRAGDROP_S_DROP = 0x00040100; public const int D...
相关文章
文章评论
共有0条评论来说两句吧...
文章二维码
点击排行
-
Docker使用Oracle官方镜像安装(12C,18C,19C)
- Springboot2将连接池hikari替换为druid,体验最强大的数据库连接池
- CentOS8编译安装MySQL8.0.19
- Docker快速安装Oracle11G,搭建oracle11g学习环境
- SpringBoot2配置默认Tomcat设置,开启更多高级功能
- MySQL8.0.19开启GTID主从同步CentOS8
- CentOS7,8上快速安装Gitea,搭建Git服务器
- Jdk安装(Linux,MacOS,Windows),包含三大操作系统的最全安装
- SpringBoot2编写第一个Controller,响应你的http请求并返回结果
推荐阅读
最新文章
- SpringBoot2初体验,简单认识spring boot2并且搭建基础工程
- Mario游戏-低调大师作品
- CentOS6,7,8上安装Nginx,支持https2.0的开启
- Springboot2将连接池hikari替换为druid,体验最强大的数据库连接池
- Eclipse初始化配置,告别卡顿、闪退、编译时间过长
- SpringBoot2整合Thymeleaf,官方推荐html解决方案
- Hadoop3单机部署,实现最简伪集群
- 设置Eclipse缩进为4个空格,增强代码规范
- CentOS7编译安装Gcc9.2.0,解决mysql等软件编译问题
- Docker使用Oracle官方镜像安装(12C,18C,19C)