Question

I'm trying something with TabControl and am flummoxed by this

<TabControl Name="MainControl">
    <TabItem Header="MyTab1">
    <TextBlock Text="{Binding  Path=SelectedItem.Header, RelativeSource={RelativeSource FindAncestor, AncestorType=TabControl}}"/>
    </TabItem>
    <TabItem Header="Modules" ContentTemplate="{StaticResource tabItemControl}"/>
</TabControl>

this works as expected.

However this :

<TabControl Name="MainControl">
    <TabItem Header="MyTab1">
    <TextBlock Text="{Binding  Path=Header, RelativeSource={RelativeSource FindAncestor, AncestorType=TabItem}}"/>
    </TabItem>
    <TabItem Header="Modules" ContentTemplate="{StaticResource tabItemControl}"/>
</TabControl>

does not.

Any idea why?

Is the selectedItem not a TabItem? Is there something different in the hierarchy?

Was it helpful?

Solution

As shown in the post RelativeSource in DataTemplate works with TabControl but not with TabItem the TabItem and SelectedContent are hosted in different panels hence SelectedItem.Header is not working as it's not actually a tabItem

Posting the snoop image from the earlier post below enter image description here

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top