none
ItemsControl inside a TabControl - how to catch a selection change RRS feed

  • Question

  • Hi all,

    I have a TabControl to display a collection of objects. Since those objects are a little to complex, i decided to use a DataTemplate (with a TabControl) to display each of them.
    This way I ended with a TabControl inside another TabControl.

    My problem is:
    When I select a new object (from the parent TabControl), I want to select the Item with index = 0 from the tabControl inside the DataTemplate (the child TabControl).

    Failed tries:
    • Catch the selection changed from the parent control. I can't reference the child tabControl since it is inside a DataTemplate and LogicalTreeHelper doesn't like DataTemplates since they don't have childs on the LogicalTree.
    • Catch RequestBringIntoView in the child control. Only procs when I change an Item on the child control, never on the parent.
    I also tried some desperated tries like findResource or findName but none of them worked =/

    Does anybody have a clue how can I reference the tabControl inside the DataTemplate when a new Item is selected?

    Thanks

    Daniel
    Friday, February 6, 2009 3:13 PM

Answers

  • TabControl is an ItemsControl, so you have SelectedItem, SelectionChanged event, Item, ItemsContainerGenerator, and so on. And it has TabItems inside. I think we can make something with this.

    Let me think... I suppose you have a collection of data that binds to the parent TabControl. Each object in this collection has its own collection inside that eventually binds to every internal TabControl. I think the best course of action is to create an IsSelected property in your ViewModel that binds to the TabItem IsSelected property. To do this you must define a simple style on your resources for your TabItems and assign it to ItemContainerStyle in your TabControl. Something like this:

        <Style x:Key="TabItemStyle" 
               TargetType="{x:Type TabItem}"
            <Setter Property="IsSelected" 
                    Value="{Binding IsSelected, Mode=TwoWay}" /> 
        </Style> 
     
     
    <TabControl ItemContainerStyle={StaticResource TabItemStyle} 
                ...> 
    </TabControl> 

    Now every object on your side know when it's being selected and unselected. From this point on it's easy, on your IsSelected property you can go to your first internal object and assign its own IsSelected property to true, everything will take care of itself afterwards.

    Hope this helps.

     

    Regards,

    -Sergi Díaz

     

     

     

    • Marked as answer by dcarneiro Friday, February 6, 2009 4:18 PM
    Friday, February 6, 2009 3:31 PM

All replies

  • TabControl is an ItemsControl, so you have SelectedItem, SelectionChanged event, Item, ItemsContainerGenerator, and so on. And it has TabItems inside. I think we can make something with this.

    Let me think... I suppose you have a collection of data that binds to the parent TabControl. Each object in this collection has its own collection inside that eventually binds to every internal TabControl. I think the best course of action is to create an IsSelected property in your ViewModel that binds to the TabItem IsSelected property. To do this you must define a simple style on your resources for your TabItems and assign it to ItemContainerStyle in your TabControl. Something like this:

        <Style x:Key="TabItemStyle" 
               TargetType="{x:Type TabItem}"
            <Setter Property="IsSelected" 
                    Value="{Binding IsSelected, Mode=TwoWay}" /> 
        </Style> 
     
     
    <TabControl ItemContainerStyle={StaticResource TabItemStyle} 
                ...> 
    </TabControl> 

    Now every object on your side know when it's being selected and unselected. From this point on it's easy, on your IsSelected property you can go to your first internal object and assign its own IsSelected property to true, everything will take care of itself afterwards.

    Hope this helps.

     

    Regards,

    -Sergi Díaz

     

     

     

    • Marked as answer by dcarneiro Friday, February 6, 2009 4:18 PM
    Friday, February 6, 2009 3:31 PM
  • That worked just fine.

    Thanks a lot
    Friday, February 6, 2009 4:17 PM