1. public class UIHelper
  2. {
  3. /// <summary>
  4. /// 在Visual里找到想要的元素
  5. /// childName可为空,不为空就按名字找
  6. /// </summary>
  7. public static T FindChild<T>(DependencyObject parent, string childName)
  8. where T : DependencyObject
  9. {
  10. if (parent == null) return null;
  11.  
  12. T foundChild = null;
  13.  
  14. int childrenCount = VisualTreeHelper.GetChildrenCount(parent);
  15. for (int i = 0; i < childrenCount; i++)
  16. {
  17. var child = VisualTreeHelper.GetChild(parent, i);
  18.  
  19. T childType = child as T;
  20. if (childType == null)
  21. {
  22. // 住下查要找的元素
  23. foundChild = FindChild<T>(child, childName);
  24.  
  25. // 如果找不到就反回
  26. if (foundChild != null) break;
  27. }
  28. else if (!string.IsNullOrEmpty(childName))
  29. {
  30. var frameworkElement = child as FrameworkElement;
  31. // 看名字是不是一样
  32. if (frameworkElement != null && frameworkElement.Name == childName)
  33. {
  34. //如果名字一样返回
  35. foundChild = (T)child;
  36. break;
  37. }
  38. }
  39. else
  40. {
  41. // 找到相应的元素了就返回
  42. foundChild = (T)child;
  43. break;
  44. }
  45. }
  46.  
  47. return foundChild;
  48. }
  49.  
  50. /// <summary>
  51. /// 得到指定元素的集合
  52. /// </summary>
  53. /// <typeparam name="T"></typeparam>
  54. /// <param name="depObj"></param>
  55. /// <returns></returns>
  56. public static IEnumerable<T> FindVisualChildren<T>(DependencyObject depObj)
  57. where T : DependencyObject
  58. {
  59. if (depObj != null)
  60. {
  61. for (int i = 0; i < VisualTreeHelper.GetChildrenCount(depObj); i++)
  62. {
  63. DependencyObject child = VisualTreeHelper.GetChild(depObj, i);
  64. if (child != null && child is T)
  65. {
  66. yield return (T)child;
  67. }
  68.  
  69. foreach (T childOfChild in FindVisualChildren<T>(child))
  70. {
  71. yield return childOfChild;
  72. }
  73. }
  74. }
  75. }
  76. }

  

  1. public class FocusAdvancement
  2. {
  3. public static bool GetAdvancesByEnterKey(DependencyObject obj)
  4. {
  5. return (bool)obj.GetValue(AdvancesByEnterKeyProperty);
  6. }
  7.  
  8. public static void SetAdvancesByEnterKey(DependencyObject obj, bool value)
  9. {
  10. obj.SetValue(AdvancesByEnterKeyProperty, value);
  11. }
  12.  
  13. public static readonly DependencyProperty AdvancesByEnterKeyProperty =
  14. DependencyProperty.RegisterAttached("AdvancesByEnterKey", typeof(bool), typeof(FocusAdvancement),
  15. new UIPropertyMetadata(OnAdvancesByEnterKeyPropertyChanged));
  16.  
  17. static void OnAdvancesByEnterKeyPropertyChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
  18. {
  19. var element = d as UIElement;
  20. if (element == null) return;
  21.  
  22. if ((bool)e.NewValue) element.KeyDown += Keydown;
  23. else element.KeyDown -= Keydown;
  24. }
  25.  
  26. static void Keydown(object sender, KeyEventArgs e)
  27. {
  28. if (!e.Key.Equals(Key.Enter)) return;
  29.  
  30. var element = sender as UIElement;
  31.  
  32. if (element != null) element.MoveFocus(new TraversalRequest(FocusNavigationDirection.Next));
  33. }
  34.  
  35. }
  1. xmlns:FOU="clr-namespace:WPFAppVM.AppViews.AppCustomViews"
  2.  
  3. <TextBox FOU:FocusAdvancement.AdvancesByEnterKey="True" Width="120"></TextBox>
  4. <TextBox IsReadOnly="True" FOU:FocusAdvancement.AdvancesByEnterKey="True" Width="120"></TextBox>
  5. <Button Height="30" Width="80" FOU:FocusAdvancement.AdvancesByEnterKey="True">保存</Button>

  

WPF 得到子指定元素方法和得到指定子元素集合方法MvvM得到焦点的更多相关文章

  1. CSS关于子元素设置了float属性后父元素高度为0的解释和解决方法

    <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/stri ...

  2. 关于opacity透明度子元素继承现象的若干研究以及hack方法

    [感想]信息时代的信息是有时效性的,今天是确确实实感受到了.互联网资料虽然丰富,但是质量不一,还有大量的跟风雷同,很多人都是随手拷贝过来,根本没有实践.以前端为例,这两年浏览器的迅猛发展,造成很多原有 ...

  3. WPF编程,指定窗口图标、窗口标题,使得在运行状态下任务栏显示窗口图标的一种方法。

    原文:WPF编程,指定窗口图标.窗口标题,使得在运行状态下任务栏显示窗口图标的一种方法. 版权声明:我不生产代码,我只是代码的搬运工. https://blog.csdn.net/qq_4330793 ...

  4. JS对象 指定分隔符连接数组元素join() join()方法用于把数组中的所有元素放入一个字符串。元素是通过指定的分隔符进行分隔的。

    指定分隔符连接数组元素join() join()方法用于把数组中的所有元素放入一个字符串.元素是通过指定的分隔符进行分隔的. 语法: arrayObject.join(分隔符) 参数说明: 注意:返回 ...

  5. LINQ之路15:LINQ Operators之元素运算符、集合方法、量词方法

    本篇继续LINQ Operators的介绍,包括元素运算符/Element Operators.集合方法/Aggregation.量词/Quantifiers Methods.元素运算符从一个sequ ...

  6. JavaScript从数组中删除指定值元素的方法

    本文实例讲述了JavaScript从数组中删除指定值元素的方法.分享给大家供大家参考.具体分析如下: 下面的代码使用了两种方式删除数组的元素,第一种定义一个单独的函数,第二种为Array对象定义了一个 ...

  7. 子元素margin-top属性传递给父元素的问题 转!

    问题描述:一个父包含框包含一个子元素.给正常流的子元素一个垂直外边距margin-top就会使得父元素跟着往下走,而子元素和父元素的边距则没有发生变化. html结构:<div class=&q ...

  8. 5种方法去掉HTML中Inline-Block元素之间的空白

    5种方法去掉HTML中Inline-Block元素之间的空白 记得年轻时我在IE6上开发,绝望的希望IE6能支持display: inline-block功能.当需要在”inline”元素上控制mar ...

  9. 利用 Python + Selenium 实现对页面的指定元素截图(可截长图元素)

    对WebElement截图 WebDriver.Chrome自带的方法只能对当前窗口截屏,且不能指定特定元素.若是需要截取特定元素或是窗口超过了一屏,就只能另辟蹊径了. WebDriver.Phant ...

随机推荐

  1. mysql登录和连接 权限

    在一些配置中会要求登录mysql 授权的时候注意ip地址是ip地址,localhost是localhost,在grant授权时,如果用localhost,就必须在所登录的配置文件中使用localhos ...

  2. 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 ...

  3. eclipse中基于maven构建的web项目pom.xml中指定的jar包无法发布到tomcat中

    eclipse运行maven web项目报错: 信息: Starting Servlet Engine: Apache Tomcat/7.0.57 一月 07, 2015 11:50:44 下午 or ...

  4. BusyBox Init

    嵌入式系统内核启动后的第一个程序就是init,一般位于/sbin/init(一般是符号链接到/bin/busybox), 但有些也会直接放在根目录下如linuxrc,busybox的init不支持多级 ...

  5. httpclient访问网站时设置Accept-Encoding为gzip,deflate返回的结果为乱码的问题

    近期迷恋上httpclient模拟各种网站登陆,浏览器中的开发者工具中查看请求头信息,然后照葫芦画瓢写到httpclient的请求中去,requestheader中有这么一段设置: Accept-En ...

  6. 探索 OpenStack 之(15):oslo.messaging 和 Cinder 中 MessageQueue 消息的发送和接收

    前言:上一篇文章 只是 RabbitMQ 的科普,本文将仔细分析 Cinder 中 RabbitMQ 的各组件的使用.消息的发送和接收等.由于各流程步骤很多,本文只会使用若干流程图来加以阐述,尽量做到 ...

  7. jmap,jhat分析内存

    分析JAVA Application的内存使用时,jmap是一个很实用的轻量级工具.使用jmap可以查看heap空间的概要情况,粗略的掌握heap的使用情况.也可以生成heapdump文件,再使用jh ...

  8. uva 297 quadtrees——yhx

    Quadtrees  A quadtree is a representation format used to encode images. The fundamental idea behind ...

  9. Hog SVM 车辆 行人检测

    HOG SVM 车辆检测 近期需要对卡口车辆的车脸进行检测,首先选用一个常规的检测方法即是hog特征与SVM,Hog特征是由dalal在2005年提出的用于道路中行人检测的方法,并且取的了不错的识别效 ...

  10. 怎样用ZBrush中复数对象进行工作

    在ZBrush®中有两种方法可以使用复数对象即“多边形组”和“次工具”. 若有疑问可直接访问:http://www.zbrushcn.com/jichu/fushu-duixiang.html 什么是 ...