I have the following C# code to find children of a DepedendencyObject:
public static IEnumerable<T> FindVisualChildren<T>(DependencyObject parent) where T : DependencyObject
{
int childrenCount = VisualTreeHelper.GetChildrenCount(parent);
for (int i = 0; i < childrenCount; i++)
{
var child = VisualTreeHelper.GetChild(parent, i);
T childType = child as T;
if (childType == null)
{
foreach (var other in FindVisualChildren<T>(child))
yield return other;
}
else
{
yield return (T)child;
}
}
}
When I loop through the TabItems in the XAML posted at the bottom, passing each TabItem to the above method, asking it to find all Expanders, it returns nothing. Also, I am making this request in an event handler attached to the Loaded event of each tab item.
<TextBlock Text="Number of Parts" Grid.Column="0"/>
<ComboBox Grid.Column="2"
Margin="0,0,0,2"
/>
</Grid>
</Expander>
<Expander Header="Date/Time Format"
Margin="5,0,5,0"
Padding="3,3,0,0"
IsExpanded="True" >
<Grid Margin="20,4,0,4">
<Grid.RowDefinitions>
<RowDefinition Height="25"/>
<RowDefinition Height="Auto"/>
</Grid.RowDefinitions>
<Grid.ColumnDefinitions>
<ColumnDefinition/>
<ColumnDefinition Width="*" />
</Grid.ColumnDefinitions>
<TextBlock Text="Date/Time Format" Grid.Row="0"/>
<ComboBox Name="cmbDateTimeFormats"
Grid.Row="0" Grid.Column="2"/>
</Grid>
</Expander>
</StackPanel>
</DockPanel>
</Border>
</TabItem>
<TabItem Header="Profile">
<Border >
<DockPanel LastChildFill="False">
<StackPanel DockPanel.Dock="Top">
<GroupBox Header="Local"
Margin="5,8" Padding="3,3,0,0"
>
<Grid Margin="20,4,0,4">
<Grid.RowDefinitions>
<RowDefinition Height="25"/>
<RowDefinition Height="Auto"/>
<RowDefinition Height="Auto"/>
<RowDefinition Height="Auto"/>
</Grid.RowDefinitions>
<Grid.ColumnDefinitions>
<ColumnDefinition />
<ColumnDefinition />
<ColumnDefinition />
<ColumnDefinition Width="*" />
</Grid.ColumnDefinitions>
<Button Content="Location..." Grid.Row="0" Name="btnProfLoc" />
<TextBlock Text="{Binding ProfileLocation}" Grid.Row="0" Grid.Column="2"/>
<Button Name="btnSaveProfile" Height="25"
Margin="2,5,0,0" Grid.Row="1"
Padding="2,1" >
<StackPanel Orientation="Horizontal">
<TextBlock Text="Save" Margin="5,0"/>
</StackPanel>
</Button>
<Button Name="btnLoadProfile" Height="25"
Margin="2,5,0,0" Grid.Row="2"
Padding="2,1" >
<StackPanel Orientation="Horizontal">
<TextBlock Text="Load" Margin="5,0"/>
</StackPanel>
</Button>
<Button Name="btnResetProfile" Height="25"
Margin="2,5,0,0" Grid.Row="3"
Padding="2,1" >
<StackPanel Orientation="Horizontal">
<TextBlock Text="Reset" Margin="5,0"/>
</StackPanel>
</Button>
</Grid>
</GroupBox>
</StackPanel>
<StackPanel
DockPanel.Dock="Bottom" Orientation="Horizontal">
</StackPanel>
</DockPanel>
</Border>
</TabItem>
</TabControl>
Any guesses what is wrong with my approach? I have not tried in this particular custom control but this method has been used to find children of a given type in another custom control. Main difference is that the items I am looking for are children of TabItems.