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 ...
 
随机推荐
- Node.js(1)-helloworld
			
1.Node.Js 环境准备 下载地址: node.js http://nodejs.org/download/ vs.net集成开发环境: Node.js Tools for Visual St ...
 - Buffer篇
			
// var buf1 = new Buffer(26);/*返回一个新的buffer对象,这个新buffer和老buffer公用一个内存.但是被start和end索引偏移缩减了.(比如,一个buff ...
 - 浅谈Python时间模块
			
浅谈Python时间模块 今天简单总结了一下Python处理时间和日期方面的模块,主要就是datetime.time.calendar三个模块的使用.希望这篇文章对于学习Python的朋友们有所帮助 ...
 - C语言流程控制
			
顺序结构 顺序结构是最常用的结构,即从上到下的执行语句. int num=5; num++; num=13; 条件结构 条件结构是当表达式为真的时候执行语句块,C语言提供了两种条件结构 if...el ...
 - (转载)web测试方法总结
			
web测试方法总结 一.输入框 1.字符型输入框: (1)字符型输入框:英文全角.英文半角.数字.空或者空格.特殊字符“~!@#¥%……&*?[]{}”特别要注意单引号和&符号.禁止直 ...
 - android:layout_gravity和android:gravity的区别
			
1.首先来看看android:layout_gravity和android:gravity的使用区别. android:gravity: 这个是针对控件里的元素来说的,用来控制元素在该控件里的显示位置 ...
 - 计算几何--求凸包模板--Graham算法--poj 1113
			
Wall Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 28157 Accepted: 9401 Description ...
 - poj2528 Mayor's posters(线段树之成段更新)
			
Mayor's posters Time Limit: 1000MSMemory Limit: 65536K Total Submissions: 37346Accepted: 10864 Descr ...
 - mysql force index() 强制索引的使用
			
mysql force index() 强制索引的使用 之前跑了一个SQL,由于其中一个表的数据量比较大,而在条件中有破坏索引或使用了很多其他索引,就会使得sql跑的非常慢... 那我们怎么解决呢? ...
 - C++find函数
			
头文件 #include <algorithm> 函数实现 template<class InputIterator, class T> InputIterator find ...