Search code examples
wpfuser-controlstabcontroltabitemvisual-tree

WPF TabControl Children


This is my current Scenario: I have several UserControls inside different TabItems on a single TabControl in a WPF Window. Something Like:

<Window x:Class="MainWindow"  
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"  
Title="S.C.A.R" WindowState="Maximized">  
    <TabControl Name="MainTabControl">  
        <TabItem Name="TabOps">  
            <Grid>  
                <Grid.RowDefinitions>  
                    <RowDefinition/>  
                    <RowDefinition/>  
                    <RowDefinition/>  
                    <RowDefinition Height="20"/>  
                </Grid.RowDefinitions>  
                <Grid.ColumnDefinitions>  
                    <ColumnDefinition/>  
                    <ColumnDefinition Width="30"/>  
                    <ColumnDefinition/>  
                </Grid.ColumnDefinitions>  
                <Local:ServiceList Height="Auto" CanInsert="True" CanCollapse="True" Grid.ColumnSpan="3" x:Name="SL" RecordState="Edit"/>  
                <Local:ReservationList CanCollapse="True" Grid.Row="1"  RecordState="Edit" x:Name="RL"/>  
                <Local:DriverList CanDelete="False" CanInsert="False"   CanCollapse="True" Grid.Row="1" Grid.Column="2" RecordState="Edit" x:Name="DL"/>  
                <Local:CustomerForm CanDelete="False" CanInsert="False" Grid.Row="2"   Grid.ColumnSpan="3" RecordState="View" x:Name="CL"/>  
                </Grid>  
        </TabItem>  
        <TabItemItem Name="TabCodes">  
                <Local:CustomerList x:Name="CustomerCRUD" RecordState="View"/>  
         </TabItem>  
        <Button Grid.Row="1"  Content="TEST" Click="Button_Click"/>  
    </Grid>  
</Border>  
</Window>  

Sorry for the indentation. For some reason I can't get the code properly indented here :(

What I need to do is to determine (preferably in the TabControl.Load Method, which of my different UserControls are currently visible. I need to do this in a dynamic way, I cannot hardcode the relationship between the TabItems and their children, something like: if (TabControl.SelectedItem is XXXX)... is not possible here, because this is a Dynamic UI and I have no way to know which controls are there up front.

I've been digging a little bit and found out that the TabItem controls do not appear in the Visual tree of their "children". I only see a ContentPresenter, and then the TabControl itself. It looks like the tabItems do not "contain" their own content, so I could not, for example, do a FindAncestor to the Tab Items.

Another interesting fact is that the Loaded event of my usercontrols is being called on startup. Regardless of whether or not they're visible on screen.

An ideal scenario will be to find an event that is only fired on my Usercontrols when the TabItem they are under gets selected.

Appreciate any ideas. Thanks in advance


Solution

  • You should be able to leverage the VisualTreeHelper and consequentrly this answer on SO to provide the TabItem.Content returned object and look for the your specified type, UserControl in this instance.

    NOTE:

    For additional details please see the comments which transpired in the SO's question.