2

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

Dave Clemmer
  • 3,741
  • 12
  • 49
  • 72
Federico Berasategui
  • 43,562
  • 11
  • 100
  • 154
  • Are you equating with currently visible to a given TabItem being selected? If so then dynamic or not you can get what you need based on the selected tab. – Aaron McIver Mar 16 '11 at 17:30
  • I've tried that, but since the TabItems do not appear in the Visual Tree of the user controls, I can't find a way to find out whether or not the currently selected Tab Item is the one my Usercontrols belong to. Therefore I cannot determine which usercontrols I need to provision (by performing queries on the Business Layer to bring the data into the screen.) – Federico Berasategui Mar 16 '11 at 17:54
  • @HighCore Are the TabItems containers for the UserControls where the UserControl is being stored within the Content property of the TabItem? – Aaron McIver Mar 16 '11 at 17:59
  • The UserControls will always be "inside" the TabItems, they will always be inside the visual tree of the TabItems' content, however, the statement "TabItem.Content == MyUserControl" is not always True, in fact, most probably there will be a Grid, or StackPanel, or DockPanel, or any other container in between. – Federico Berasategui Mar 16 '11 at 18:09
  • This is the reason why I cannot do something like "TabItem.GetAllUserControls", because there's no way to determine where in the Visual Tree this controls will be located, it may happen to have a Grid, with a dockpanel inside, then a second grid, then the UserControl – Federico Berasategui Mar 16 '11 at 18:11
  • 1
    @HighCore If it is always within the Content property (regardless of level) then hunt the TabItem, using something like this...http://stackoverflow.com/questions/974598/find-all-controls-in-wpf-window-by-type where you provide the TabItem.Content returned object and look for the your specified type, UserControl in this instance. – Aaron McIver Mar 16 '11 at 18:16
  • Great! thanks! I was just looking for something like this, I had found a similar solution, but it was a lot of code, and I didn't like that from a performance perspective. This is very straightforward. Thanks! – Federico Berasategui Mar 16 '11 at 18:46
  • @HighCore Added as answer for those that stumble upon this question; yw – Aaron McIver Mar 16 '11 at 19:24

1 Answers1

1

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.

Community
  • 1
  • 1
Aaron McIver
  • 24,527
  • 5
  • 59
  • 88