I am trying to loop through child elements of a Tab Control to know what Check Boxes are set to checked or not checked. I have found various answers on SO, but I can't seem to get the code to do what I need. Here is what I have thus far:
foreach (System.Windows.Controls.TabItem page in this.MainWindowTabControl.Items)
{
if(VisualTreeHelper.GetChild(page, 0).GetType() == typeof(System.Windows.Controls.Grid))
{
var grid = VisualTreeHelper.GetChild(page, 0);
int gridChildCount = VisualTreeHelper.GetChildrenCount(grid);
for(int i = 0; i < gridChildCount; i++)
{
if(VisualTreeHelper.GetChild(grid, i).GetType() == typeof(CheckBox))
{
CheckBox box = (CheckBox)VisualTreeHelper.GetChild(grid, i);
if (boxer.IsChecked == true)
checkboxes.Add(box);
}
}
//do work
}
}
Most likely, I am thinking incorrectly about how the VisualTreeHelper class works. I imagine I can keep working though the XAML Code to keep moving into deeper and deeper children of the Tab Control? Currently, my code on my WPF's xaml looks like this:
<TabControl x:Name="MainWindowTabControl" HorizontalAlignment="Left" Height="470"
Margin="0,10,0,0" VerticalAlignment="Top" Width="1384">
<TabItem Header="TabItem">
<Grid Background="#FFE5E5E5" Margin="0,-21,0,0">
<CheckBox Name="testBox" Content="Check Box"
HorizontalAlignment="Left" VerticalAlignment="Top" Margin="1293,50,0,0"/>
</Grid>
</TabItem>
</TabControl>
So, my understanding is that I have to work from child to child, meaning, use the VisualTreeHelper to get the Children of the Tab Control (select Tab Item), then get the children of the TabItem (select the grid), then get the children of Grid, and then I can finally loop through the children (checkboxes) to get the information I want. If I am mistaken can someone please explain where I am going wrong?
EDIT: Changed Checkbox XAML to the proper code