Question

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
                }
Was it helpful?

Solution

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

OTHER TIPS

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()

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top