How can I loop through all of the controls in a tab control (wpf)?
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 th开发者_C百科e 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 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";
}
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()
精彩评论