views:

286

answers:

3

I have seen a few examples of how to do this with winforms but have been unable to get it to work in wpf as a wpf TabItem does not have a definition for Controls. Here is the code that I'm using right now, which does not work.

  TabItem ti = rep1Tab;
                var controls = ti.Controls;
                foreach (var control in controls)
                {
                    //do stuff
                }
A: 

Does this work?

ti.Content.LogicalChildren
Leigh Shayler
No I'm still getting an error saying: "'object' does not contain a definition for 'LogicalChildren' and no extension method 'LogicalChildren' accepting a first argument of type 'object' could be found (are you missing a using directive or an assembly reference?)"
Justin
+3  A: 

If you want the logical children, you use LogicalTreeHelper.GetChilren(). If you want the visual children, you use VisualTreeHelper.GetChild() in conjunction with VisualTreeHelper.GetChildrenCount()

bitbonk
+4  A: 

A TabItem usually contains a container control such as the default Grid. You can try looping through the children of that container control.

foreach (UIElement element in Grid1.Children)
        {
            //process element
        }

If you have to access properties of a particular control, you have to convert the element

 foreach (UIElement element in Grid1.Children)
        {
            //process element
            Button btn = (Button)element;
            btn.Content = "Hello World";
        }
Jacob Reyes