VisualTreeHelper.GetChildren does not find children of TabItem

Klaus Nji picture Klaus Nji · Aug 24, 2012 · Viewed 9.5k times · Source

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.

Answer

Nigel Thorne picture Nigel Thorne · Mar 26, 2013

The controls within a tab don't seem to be the children of a TabItem in the visual tree. They are the children of a TabControl.

You can see what I mean if you add the following code to your app.. and include a button on the tab with a click handler that reports the button's Path.

public string Id(object control)
{
    if (control is UIElement)
    {
        string id = ((UIElement)control).GetValue(AutomationProperties.AutomationIdProperty).ToString();
        id += "(" + control.GetType().Name + ")";
        return id;
    }
    return "not a ui element";
}

private static T FindParent<T>(DependencyObject child)
    where T : DependencyObject
{
    if (child == null) return null; 
    var parent = VisualTreeHelper.GetParent(child);
    return parent as T ?? FindParent<T>(parent);
}

public string Path(object control)
{
    if ( control == null ) return "";
    var path = Id(control);
    var parent = FindParent<FrameworkElement>(control as UIElement);
    if (parent != null ) path = Path(parent) +"/"+ path;
    return path;
}

For my application I get the following: "MainForm(MainPage)/(Grid)/(StackPanel)/TabControl(TabControl)/(Grid)/(Grid)/(Border)/(ContentPresenter)/(StackPanel)/Button(Button)"

Notice the TabControl, but no TabItem.

If I hook up to events from the TabItem itself I get the following path: "MainForm(MainPage)/(Grid)/(StackPanel)/TabControl(TabControl)/(Grid)/(Grid)/(TabPanel)/MyTabItem(TabItem)"

This shows the items don't exist within the TabItem in the visual tree, but as children of the TabControl. (Which sucks.) Note: they are virtualized and realized as you change tabs.