获取并通过控制从TabItem的迭代? [英] Get and Iterate through Controls from a TabItem?

查看:140
本文介绍了获取并通过控制从TabItem的迭代?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何获得所有这些嵌套在一个TabItem的(从一个TabControl)的控制/ UI元素?



我什么都试过,但没能得到他们。



(设置SelectedTab):

 私人TabItem的SelectedTab = NULL ; 
私人无效tabControl1_SelectionChanged(对象发件人,SelectionChangedEventArgs E)
{
SelectedTab =(TabItem的)tabControl1.SelectedItem;
}

现在我需要的是这样的:

 私人的StackPanel theStackPanelInWhichLabelsShouldBeLoaded = NULL; 
的foreach(在tabControl.Children控制控制/ *犯规存在* /或tabControl.Items / *只的TabItems * /或/*SelectedTab.Items ?? * /)//我没有计划
{
如果(控制StackPanel中)
{
theStackPanelInWhichLabelsShouldBeLoaded =控制; $ B $在StackPanel中b //加载标签,那是没有问题的工作
}
}

Silvermind后:
这样做,伯爵总是1:

  UpdateLayout请(); 
INT nChildCount = VisualTreeHelper.GetChildrenCount(SelectedTab);


解决方案

的TabControl有项目属性(自ItemsControl派生),这返回所有的TabItems - http://msdn.microsoft .COM / EN-US /库/ system.windows.controls.itemscontrol.items.aspx 。或者你也可以遍历可视树:



VAR firstStackPanelInTabControl = FindVisualChildren(TabControl的)。首先();

 公共静态的IEnumerable< T> FindVisualChildren< T>(DependencyObject的rootObject)其中T:DependencyObject的
{
如果(!rootObject = NULL)
{
的for(int i = 0; I< VisualTreeHelper.GetChildrenCount (rootObject);我++)
{
DependencyObject的孩子= VisualTreeHelper.GetChild(rootObject,I);

如果(孩子= NULL&放大器;!&安培;孩子是T)
收益回报(T)的孩子;

的foreach(T childOfChild在FindVisualChildren< T>(子))
收益率的回报childOfChild;
}
}
}


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 SelectedTab):

    private TabItem SelectedTab = null;
    private void tabControl1_SelectionChanged(object sender, SelectionChangedEventArgs e)
    {
        SelectedTab = (TabItem)tabControl1.SelectedItem;
    }

Now I need something like this:

  private StackPanel theStackPanelInWhichLabelsShouldBeLoaded = null;
  foreach (Control control in tabControl.Children /*doesnt exist*/, or tabControl.Items /*only TabItems*/, or /*SelectedTab.Items ??*/ ) //I Have no plan
  {
        if(control is StackPanel)
        {
            theStackPanelInWhichLabelsShouldBeLoaded = control;
            //Load Labels in the Stackpanel, thats works without problems
        }
  }

After Silvermind: Doing this, the Count is always 1:

        UpdateLayout();
        int nChildCount = VisualTreeHelper.GetChildrenCount(SelectedTab);

解决方案

TabControl has Items property (derived from ItemsControl), which returns all TabItems - http://msdn.microsoft.com/en-us/library/system.windows.controls.itemscontrol.items.aspx. Or you can traverse visual tree:

var firstStackPanelInTabControl = FindVisualChildren(tabControl).First();

public static IEnumerable<T> FindVisualChildren<T>(DependencyObject rootObject) where T : DependencyObject
{
  if (rootObject != null)
  {
    for (int i = 0; i < VisualTreeHelper.GetChildrenCount(rootObject); i++)
    {
      DependencyObject child = VisualTreeHelper.GetChild(rootObject, i);

      if (child != null && child is T)
        yield return (T)child;

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

这篇关于获取并通过控制从TabItem的迭代?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆