WPF 得到子指定元素方法和得到指定子元素集合方法MvvM得到焦点
- public class UIHelper
- {
- /// <summary>
- /// 在Visual里找到想要的元素
- /// childName可为空,不为空就按名字找
- /// </summary>
- public static T FindChild<T>(DependencyObject parent, string childName)
- where T : DependencyObject
- {
- if (parent == null) return null;
- T foundChild = null;
- int childrenCount = VisualTreeHelper.GetChildrenCount(parent);
- for (int i = 0; i < childrenCount; i++)
- {
- var child = VisualTreeHelper.GetChild(parent, i);
- T childType = child as T;
- if (childType == null)
- {
- // 住下查要找的元素
- foundChild = FindChild<T>(child, childName);
- // 如果找不到就反回
- if (foundChild != null) break;
- }
- else if (!string.IsNullOrEmpty(childName))
- {
- var frameworkElement = child as FrameworkElement;
- // 看名字是不是一样
- if (frameworkElement != null && frameworkElement.Name == childName)
- {
- //如果名字一样返回
- foundChild = (T)child;
- break;
- }
- }
- else
- {
- // 找到相应的元素了就返回
- foundChild = (T)child;
- break;
- }
- }
- return foundChild;
- }
- /// <summary>
- /// 得到指定元素的集合
- /// </summary>
- /// <typeparam name="T"></typeparam>
- /// <param name="depObj"></param>
- /// <returns></returns>
- public static IEnumerable<T> FindVisualChildren<T>(DependencyObject depObj)
- where T : DependencyObject
- {
- if (depObj != null)
- {
- for (int i = 0; i < VisualTreeHelper.GetChildrenCount(depObj); i++)
- {
- DependencyObject child = VisualTreeHelper.GetChild(depObj, i);
- if (child != null && child is T)
- {
- yield return (T)child;
- }
- foreach (T childOfChild in FindVisualChildren<T>(child))
- {
- yield return childOfChild;
- }
- }
- }
- }
- }
- public class FocusAdvancement
- {
- public static bool GetAdvancesByEnterKey(DependencyObject obj)
- {
- return (bool)obj.GetValue(AdvancesByEnterKeyProperty);
- }
- public static void SetAdvancesByEnterKey(DependencyObject obj, bool value)
- {
- obj.SetValue(AdvancesByEnterKeyProperty, value);
- }
- public static readonly DependencyProperty AdvancesByEnterKeyProperty =
- DependencyProperty.RegisterAttached("AdvancesByEnterKey", typeof(bool), typeof(FocusAdvancement),
- new UIPropertyMetadata(OnAdvancesByEnterKeyPropertyChanged));
- static void OnAdvancesByEnterKeyPropertyChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
- {
- var element = d as UIElement;
- if (element == null) return;
- if ((bool)e.NewValue) element.KeyDown += Keydown;
- else element.KeyDown -= Keydown;
- }
- static void Keydown(object sender, KeyEventArgs e)
- {
- if (!e.Key.Equals(Key.Enter)) return;
- var element = sender as UIElement;
- if (element != null) element.MoveFocus(new TraversalRequest(FocusNavigationDirection.Next));
- }
- }
- xmlns:FOU="clr-namespace:WPFAppVM.AppViews.AppCustomViews"
- <TextBox FOU:FocusAdvancement.AdvancesByEnterKey="True" Width="120"></TextBox>
- <TextBox IsReadOnly="True" FOU:FocusAdvancement.AdvancesByEnterKey="True" Width="120"></TextBox>
- <Button Height="30" Width="80" FOU:FocusAdvancement.AdvancesByEnterKey="True">保存</Button>
WPF 得到子指定元素方法和得到指定子元素集合方法MvvM得到焦点的更多相关文章
- CSS关于子元素设置了float属性后父元素高度为0的解释和解决方法
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/stri ...
- 关于opacity透明度子元素继承现象的若干研究以及hack方法
[感想]信息时代的信息是有时效性的,今天是确确实实感受到了.互联网资料虽然丰富,但是质量不一,还有大量的跟风雷同,很多人都是随手拷贝过来,根本没有实践.以前端为例,这两年浏览器的迅猛发展,造成很多原有 ...
- WPF编程,指定窗口图标、窗口标题,使得在运行状态下任务栏显示窗口图标的一种方法。
原文:WPF编程,指定窗口图标.窗口标题,使得在运行状态下任务栏显示窗口图标的一种方法. 版权声明:我不生产代码,我只是代码的搬运工. https://blog.csdn.net/qq_4330793 ...
- JS对象 指定分隔符连接数组元素join() join()方法用于把数组中的所有元素放入一个字符串。元素是通过指定的分隔符进行分隔的。
指定分隔符连接数组元素join() join()方法用于把数组中的所有元素放入一个字符串.元素是通过指定的分隔符进行分隔的. 语法: arrayObject.join(分隔符) 参数说明: 注意:返回 ...
- LINQ之路15:LINQ Operators之元素运算符、集合方法、量词方法
本篇继续LINQ Operators的介绍,包括元素运算符/Element Operators.集合方法/Aggregation.量词/Quantifiers Methods.元素运算符从一个sequ ...
- JavaScript从数组中删除指定值元素的方法
本文实例讲述了JavaScript从数组中删除指定值元素的方法.分享给大家供大家参考.具体分析如下: 下面的代码使用了两种方式删除数组的元素,第一种定义一个单独的函数,第二种为Array对象定义了一个 ...
- 子元素margin-top属性传递给父元素的问题 转!
问题描述:一个父包含框包含一个子元素.给正常流的子元素一个垂直外边距margin-top就会使得父元素跟着往下走,而子元素和父元素的边距则没有发生变化. html结构:<div class=&q ...
- 5种方法去掉HTML中Inline-Block元素之间的空白
5种方法去掉HTML中Inline-Block元素之间的空白 记得年轻时我在IE6上开发,绝望的希望IE6能支持display: inline-block功能.当需要在”inline”元素上控制mar ...
- 利用 Python + Selenium 实现对页面的指定元素截图(可截长图元素)
对WebElement截图 WebDriver.Chrome自带的方法只能对当前窗口截屏,且不能指定特定元素.若是需要截取特定元素或是窗口超过了一屏,就只能另辟蹊径了. WebDriver.Phant ...
随机推荐
- mysql登录和连接 权限
在一些配置中会要求登录mysql 授权的时候注意ip地址是ip地址,localhost是localhost,在grant授权时,如果用localhost,就必须在所登录的配置文件中使用localhos ...
- Effective Java 49 Prefer primitive types to boxed primitives
No. Primitives Boxed Primitives 1 Have their own values Have identities distinct from their values 2 ...
- eclipse中基于maven构建的web项目pom.xml中指定的jar包无法发布到tomcat中
eclipse运行maven web项目报错: 信息: Starting Servlet Engine: Apache Tomcat/7.0.57 一月 07, 2015 11:50:44 下午 or ...
- BusyBox Init
嵌入式系统内核启动后的第一个程序就是init,一般位于/sbin/init(一般是符号链接到/bin/busybox), 但有些也会直接放在根目录下如linuxrc,busybox的init不支持多级 ...
- httpclient访问网站时设置Accept-Encoding为gzip,deflate返回的结果为乱码的问题
近期迷恋上httpclient模拟各种网站登陆,浏览器中的开发者工具中查看请求头信息,然后照葫芦画瓢写到httpclient的请求中去,requestheader中有这么一段设置: Accept-En ...
- 探索 OpenStack 之(15):oslo.messaging 和 Cinder 中 MessageQueue 消息的发送和接收
前言:上一篇文章 只是 RabbitMQ 的科普,本文将仔细分析 Cinder 中 RabbitMQ 的各组件的使用.消息的发送和接收等.由于各流程步骤很多,本文只会使用若干流程图来加以阐述,尽量做到 ...
- jmap,jhat分析内存
分析JAVA Application的内存使用时,jmap是一个很实用的轻量级工具.使用jmap可以查看heap空间的概要情况,粗略的掌握heap的使用情况.也可以生成heapdump文件,再使用jh ...
- uva 297 quadtrees——yhx
Quadtrees A quadtree is a representation format used to encode images. The fundamental idea behind ...
- Hog SVM 车辆 行人检测
HOG SVM 车辆检测 近期需要对卡口车辆的车脸进行检测,首先选用一个常规的检测方法即是hog特征与SVM,Hog特征是由dalal在2005年提出的用于道路中行人检测的方法,并且取的了不错的识别效 ...
- 怎样用ZBrush中复数对象进行工作
在ZBrush®中有两种方法可以使用复数对象即“多边形组”和“次工具”. 若有疑问可直接访问:http://www.zbrushcn.com/jichu/fushu-duixiang.html 什么是 ...