title: WPF TabControl Children
body: <p>This is my current Scenario: I have several UserControls inside different TabItems on a single TabControl in a WPF Window. Something Like:</p>
  
  <pre><code>&lt;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"&gt;  
      &lt;TabControl Name="MainTabControl"&gt;  
          &lt;TabItem Name="TabOps"&gt;  
              &lt;Grid&gt;  
                  &lt;Grid.RowDefinitions&gt;  
                      &lt;RowDefinition/&gt;  
                      &lt;RowDefinition/&gt;  
                      &lt;RowDefinition/&gt;  
                      &lt;RowDefinition Height="20"/&gt;  
                  &lt;/Grid.RowDefinitions&gt;  
                  &lt;Grid.ColumnDefinitions&gt;  
                      &lt;ColumnDefinition/&gt;  
                      &lt;ColumnDefinition Width="30"/&gt;  
                      &lt;ColumnDefinition/&gt;  
                  &lt;/Grid.ColumnDefinitions&gt;  
                  &lt;Local:ServiceList Height="Auto" CanInsert="True" CanCollapse="True" Grid.ColumnSpan="3" x:Name="SL" RecordState="Edit"/&gt;  
                  &lt;Local:ReservationList CanCollapse="True" Grid.Row="1"  RecordState="Edit" x:Name="RL"/&gt;  
                  &lt;Local:DriverList CanDelete="False" CanInsert="False"   CanCollapse="True" Grid.Row="1" Grid.Column="2" RecordState="Edit" x:Name="DL"/&gt;  
                  &lt;Local:CustomerForm CanDelete="False" CanInsert="False" Grid.Row="2"   Grid.ColumnSpan="3" RecordState="View" x:Name="CL"/&gt;  
                  &lt;/Grid&gt;  
          &lt;/TabItem&gt;  
          &lt;TabItemItem Name="TabCodes"&gt;  
                  &lt;Local:CustomerList x:Name="CustomerCRUD" RecordState="View"/&gt;  
           &lt;/TabItem&gt;  
          &lt;Button Grid.Row="1"  Content="TEST" Click="Button_Click"/&gt;  
      &lt;/Grid&gt;  
  &lt;/Border&gt;  
  &lt;/Window&gt;  
  </code></pre>
  
  <p>Sorry for the indentation. For some reason I can't get the code properly indented here :(</p>
  
  <p>What I need to do is to determine (preferably in the <code>TabControl.Load</code> Method, <strong>which of my different UserControls are currently visible</strong>. I need to do this in a dynamic way, I cannot hardcode the relationship between the TabItems and their children, something like: 
  <code>if (TabControl.SelectedItem is XXXX)...</code> is not possible here, because this is a Dynamic UI and I have no way to know which controls are there up front.</p>
  
  <p>I've been digging a little bit and found out that the <code>TabItem</code> controls do not appear in the Visual tree of their "children". I only see a <code>ContentPresenter</code>, and then the <code>TabControl</code> itself. It looks like the tabItems do not "contain" their own content, so I could not, for example, do a <code>FindAncestor</code> to the Tab Items.</p>
  
  <p>Another interesting fact is that the <code>Loaded</code> event of my usercontrols is being called on startup. Regardless of whether or not they're visible on screen.</p>
  
  <p>An ideal scenario will be to find an event that is only fired on my Usercontrols when the TabItem they are under gets selected.</p>
  
  <p>Appreciate any ideas. Thanks in advance</p>