按类型查找wpf窗口中的所有控件我在找一种方法来找到窗口上的所有控件,例如:找到所有TextBoxes,查找实现特定接口等的所有控件。
3 回答
万千封印
TA贡献1891条经验 获得超3个赞
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; } } }}
foreach (TextBlock tb in FindVisualChildren<TextBlock>(window)){ // do something with tb here}
胡子哥哥
TA贡献1825条经验 获得超6个赞
IEnumerable<myType> collection = control.Children.OfType<myType>();
jeck猫
TA贡献1909条经验 获得超7个赞
public static IEnumerable<T> FindLogicalChildren<T> ( DependencyObject depObj ) where T : DependencyObject { if( depObj != null ) { foreach( object rawChild in LogicalTreeHelper.GetChildren( depObj ) ){ if( rawChild is DependencyObject ) { DependencyObject child = (DependencyObject)rawChild; if( child is T ) { yield return (T)child; } foreach( T childOfChild in FindLogicalChildren<T>( child ) ) { yield return childOfChild; } } } } }
- 3 回答
- 0 关注
- 1668 浏览
添加回答
举报
0/150
提交
取消