2009-06-10 56 views
185

我正在寻找一种方式,通过他们的类型,找到窗口的所有控件,查找WPF窗口的所有控件的类型

例如:找到所有TextBoxes,发现所有的控制实现特定的接口等

+0

虽然我们的话题,这也是相关的http://咕。 gl/i9RVx – 2012-08-14 14:01:22

+0

我还写了一篇关于以下主题的博客文章:[在运行时修改ControlTemplate](http://wpfexperiments.blogspot.com/2012/08/modifying-controltemplate-in-style-at.html) – 2012-08-31 12:52:21

回答

365

这应该做的伎俩

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 
} 
+0

完美,谢谢 – 2009-06-10 23:15:31

+63

注意:如果你想让这个工作,并发现你的窗口(例如)有0可视孩子,尝试在Loaded事件处理程序中运行此方法。如果你在构造函数中运行它(甚至在InitializeComponent()之后),那么可视化子项目不会被加载,并且它不起作用。 – 2009-08-28 15:02:52

11

使用帮助类VisualTreeHelperLogicalTreeHelper,具体取决于您感兴趣的tree。它们都提供获取元素的子元素的方法(尽管语法稍有不同)。我经常使用这些类查找特定类型的第一次出现,但你可以很容易地修改它来寻找该类型的所有对象:

public static DependencyObject FindInVisualTreeDown(DependencyObject obj, Type type) 
{ 
    if (obj != null) 
    { 
     if (obj.GetType() == type) 
     { 
      return obj; 
     } 

     for (int i = 0; i < VisualTreeHelper.GetChildrenCount(obj); i++) 
     { 
      DependencyObject childReturn = FindInVisualTreeDown(VisualTreeHelper.GetChild(obj, i), type); 
      if (childReturn != null) 
      { 
       return childReturn; 
      } 
     } 
    } 

    return null; 
} 
+0

+ 1解释和帖子 但布赖斯卡尔发布的功能,充分工作 谢谢 – 2009-06-10 23:18:25

+0

这不能解决问题的问题,而且泛型类型的答案也更加清晰。将它与使用VisualTreeHelper.GetChildrenCount(obj)结合使用可以解决问题。 然而,将其视为一种选择很有用。 – 2015-10-07 12:38:33

3

,这是它如何工作的向上

private T FindParent<T>(DependencyObject item, Type StopAt) where T : class 
    { 
     if (item is T) 
     { 
      return item as T; 
     } 
     else 
     { 
      DependencyObject _parent = VisualTreeHelper.GetParent(item); 
      if (_parent == null) 
      { 
       return default(T); 
      } 
      else 
      { 
       Type _type = _parent.GetType(); 
       if (StopAt != null) 
       { 
        if ((_type.IsSubclassOf(StopAt) == true) || (_type == StopAt)) 
        { 
         return null; 
        } 
       } 

       if ((_type.IsSubclassOf(typeof(T)) == true) || (_type == typeof(T))) 
       { 
        return _parent as T; 
       } 
       else 
       { 
        return FindParent<T>(_parent, StopAt); 
       } 
      } 
     } 
    } 
59

这是最简单的方法:

IEnumerable<myType> collection = control.Children.OfType<myType>(); 

,其中控制是窗口的根元素。

4

获取特定类型的所有孩子的列表,你可以使用:

private static IEnumerable<DependencyObject> FindInVisualTreeDown(DependencyObject obj, Type type) 
{ 
    if (obj != null) 
    { 
     if (obj.GetType() == type) 
     { 
      yield return obj; 
     } 

     for (var i = 0; i < VisualTreeHelper.GetChildrenCount(obj); i++) 
     { 
      foreach (var child in FindInVisualTreeDown(VisualTreeHelper.GetChild(obj, i), type)) 
      { 
       if (child != null) 
       { 
        yield return child; 
       } 
      } 
     } 
    } 

    yield break; 
} 
7

我发现在上面的几个示例中使用的行VisualTreeHelper.GetChildrenCount(depObj),不会返回GroupBoxes的非零计数,特别是在GroupBox包含Grid的情况下,并且Grid包含子元素。我相信这可能是因为GroupBox不允许包含多个子元素,并且这个子元素存储在它的Content属性中。没有GroupBox.Children类型的属性。我相信,我不这样做非常有效,但我修改在这条产业链的第一个“FindVisualChildren”举例如下:

public IEnumerable<T> FindVisualChildren<T>(DependencyObject depObj) where T : DependencyObject 
    { 
     if (depObj != null) 
     { 
      int depObjCount = VisualTreeHelper.GetChildrenCount(depObj); 
      for (int i = 0; i <depObjCount; i++) 
      { 
       DependencyObject child = VisualTreeHelper.GetChild(depObj, i); 
       if (child != null && child is T) 
       { 
        yield return (T)child; 
       } 

       if (child is GroupBox) 
       { 
        GroupBox gb = child as GroupBox; 
        Object gpchild = gb.Content; 
        if (gpchild is T) 
        { 
         yield return (T)child; 
         child = gpchild as T; 
        } 
       } 

       foreach (T childOfChild in FindVisualChildren<T>(child)) 
       { 
        yield return childOfChild; 
       } 
      } 
     } 
    } 
4

小改动,递归这样你就可以例如找到的子标签控件一个选项卡控件。

public static DependencyObject FindInVisualTreeDown(DependencyObject obj, Type type) 
    { 
     if (obj != null) 
     { 
      for (int i = 0; i < VisualTreeHelper.GetChildrenCount(obj); i++) 
      { 
       DependencyObject child = VisualTreeHelper.GetChild(obj, i); 

       if (child.GetType() == type) 
       { 
        return child; 
       } 

       DependencyObject childReturn = FindInVisualTreeDown(child, type); 
       if (childReturn != null) 
       { 
        return childReturn; 
       } 
      } 
     } 

     return null; 
    } 
16

我适应@Bryce卡勒的回答遵循@Mathias Lykkegaard洛伦岑的建议,并使用LogicalTreeHelper。

似乎工作没关系。;!)

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; 
        } 
       } 
      } 
     } 
    } 

(它仍然不会检查标签控件或网格内GroupBoxes通过@Benjamin浆果& @大卫 - [R分别提及) (也跟着@ noonand的建议&被移去的冗余子=空)

1

这里是另一个,紧凑型,与泛型语法:

public static IEnumerable<T> FindLogicalChildren<T>(DependencyObject obj) where T : DependencyObject 
    { 
     if (obj != null) { 
      if (obj is T) 
       yield return obj as T; 

      foreach (DependencyObject child in LogicalTreeHelper.GetChildren(obj).OfType<DependencyObject>()) 
       foreach (T c in FindLogicalChildren<T>(child)) 
        yield return c; 
     } 
    } 
1

我想添加评论,但我有低于50分,所以我只能用“答案”。 请注意,如果您使用“VisualTreeHelper”方法检索XAML“TextBlock”对象,那么它也将抓取XAML“Button”对象。如果通过写入Textblock.Text参数重新初始化“TextBlock”对象,则不再能够使用Button.Content参数更改Button文本。该按钮将永久显示来自Textblock.Text写入操作写入的文字(从当它被检索 -

foreach (TextBlock tb in FindVisualChildren<TextBlock>(window)) 
{ 
// do something with tb here 
    tb.Text = ""; //this will overwrite Button.Content and render the 
       //Button.Content{set} permanently disabled. 
} 

要解决这个问题,你可以尝试使用XAML“文本框”,并添加方法( 。或活动),以模仿XAMAL按钮XAML“文本框”不为“的TextBlock”的搜索聚集

-1

我发现它更容易不可视树助手:

foreach (UIElement element in MainWindow.Children) { 
    if (element is TextBox) { 
     if ((element as TextBox).Text != "") 
     { 
      //Do something 
     } 
    } 
}; 
2

我的版本为C++/CLI

template < class T, class U > 
bool Isinst(U u) 
{ 
    return dynamic_cast<T>(u) != nullptr; 
} 

template <typename T> 
    T FindVisualChildByType(Windows::UI::Xaml::DependencyObject^ element, Platform::String^ name) 
    { 
     if (Isinst<T>(element) && dynamic_cast<Windows::UI::Xaml::FrameworkElement^>(element)->Name == name) 
     { 
      return dynamic_cast<T>(element); 
     } 
     int childcount = Windows::UI::Xaml::Media::VisualTreeHelper::GetChildrenCount(element); 
     for (int i = 0; i < childcount; ++i) 
     { 
      auto childElement = FindVisualChildByType<T>(Windows::UI::Xaml::Media::VisualTreeHelper::GetChild(element, i), name); 
      if (childElement != nullptr) 
      { 
       return childElement; 
      } 
     } 
     return nullptr; 
    }; 
2

由于某种原因,这里发布的答案都没有帮助我获得给定控件中包含的给定类型的所有控件在我的MainWindow中。 我需要在一个菜单中找到所有菜单项以迭代它们。他们并不都是菜单的直接后代,所以我设法只使用上面的任何代码来收集他们的第一个lilne。 这个扩展方法是我为这个问题的解决方案,任何人在这里都会继续阅读。

public static void FindVisualChildren<T>(this ICollection<T> children, DependencyObject depObj) where T : DependencyObject 
    { 
     if (depObj != null) 
     { 
      var brethren = LogicalTreeHelper.GetChildren(depObj); 
      var brethrenOfType = LogicalTreeHelper.GetChildren(depObj).OfType<T>(); 
      foreach (var childOfType in brethrenOfType) 
      { 
       children.Add(childOfType); 
      } 

      foreach (var rawChild in brethren) 
      { 
       if (rawChild is DependencyObject) 
       { 
        var child = rawChild as DependencyObject; 
        FindVisualChildren<T>(children, child); 
       } 
      } 
     } 
    } 

希望它有帮助。

0

真的很好的答案。

VB.NET版本:

Public Shared Iterator Function FindVisualChildren(Of T As DependencyObject)(depObj As DependencyObject) As IEnumerable(Of T) 
    If depObj IsNot Nothing Then 
     For i As Integer = 0 To VisualTreeHelper.GetChildrenCount(depObj) - 1 
      Dim child As DependencyObject = VisualTreeHelper.GetChild(depObj, i) 
      If child IsNot Nothing AndAlso TypeOf child Is T Then 
       Yield DirectCast(child, T) 
      End If 
      For Each childOfChild As T In FindVisualChildren(Of T)(child) 
       Yield childOfChild 
      Next 
     Next 
    End If 
End Function 

使用(这禁用一个窗口中的所有文本框):

 For Each tb As TextBox In FindVisualChildren(Of TextBox)(Me) 
      tb.IsEnabled = False 
     Next