Get and Iterate through Controls from a TabItem?

后端 未结 3 1238
囚心锁ツ
囚心锁ツ 2020-12-19 18:30

How to get all the Controls/UIElements which are nested in a Tabitem (from a TabControl)?

I tried everything but wasn\'t able to get them.

(Set the SelectedT

3条回答
  •  囚心锁ツ
    2020-12-19 18:48

    May be method of this kind will help you:

    public static IEnumerable FindChildren(this DependencyObject source)
                                                 where T : DependencyObject
    {
      if (source != null)
      {
        var childs = GetChildObjects(source);
        foreach (DependencyObject child in childs)
        {
          //analyze if children match the requested type
          if (child != null && child is T)
          {
            yield return (T) child;
          }
    
          //recurse tree
          foreach (T descendant in FindChildren(child))
          {
            yield return descendant;
          }
        }
      }
    }
    

    See full article (Finding Elements in the WPF Tree) here.

提交回复
热议问题