2
votes

i am using a DataBinding to show all Controls in the ItemsSource

        <TabControl Grid.Row="1" TabStripPlacement="Left" ItemsSource="{Binding Source={StaticResource WorkflowSelector}, Path=Workflows}" SelectedIndex="0">
        <TabControl.ItemTemplate>
            <DataTemplate>
                <Label Content="{Binding PluginName}"></Label>
            </DataTemplate>
        </TabControl.ItemTemplate>
    </TabControl>

the WorkflowSelector is my ViewModel which contains a List of all Controls that should be shown in the TabControl.

I created an itemTemplate to show the PluginName (public property) in the Tab but nothing is shown.

If i inspect the Visual tree i can see the Databinding of the Tabcontrol, containing 1 Item that has a Property PluginName. The evaluated Value of the BindingExpression of the Label is empty

first thing i noticed is that the ContentPresenter does not have a DataContext, while the Border does have the correct DataContext

Visual Tree of the application; ContentPresenter does not hold the DataContext

Additionally ... if i change the ItemTemplate to ContentTemplate the binding is working correctly (but in the content not in the header)

2
you want to change Header of TabITem or the TabItem itself? - StepUp
i want to change the header - Markus
I'm not sure why your code doesn't work, I think it should and this answer shows very similar code which I think works. Best guess is you have some kind of custom style applied, or perhaps an implicit DataTemplate that is interfering. Can you copy/paste your relevant parts into a new project to test? - Rachel

2 Answers

0
votes

since i saw in the Visual tree that the DataContext was set higher up in the hierarchy, i changed my data binding to:

 <Label Content="{Binding RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type TabItem}}, Path=DataContext.PluginName}"></Label>

this does not explain the root-cause, but it is a short workaround i can live with

0
votes

To change HeaderTemplate of TabControl, you should set the style of TabItem and change HeaderTemplate:

<TabControl>
        <TabControl.Resources>
            <Style TargetType="{x:Type TabPanel}">
                <Setter Property="Background" Value="Yellow"/>
            </Style>
            <Style TargetType="{x:Type TabItem}">
                <Setter Property="BorderThickness" Value="0"/>
                <Setter Property="Padding" Value="0" />
                <Setter Property="HeaderTemplate">
                    <Setter.Value>
                        <DataTemplate>
                            <Border x:Name="grid" Background="Red">
                                <ContentPresenter>
                                    <ContentPresenter.Content>
                                        <Border BorderThickness="3" BorderBrush="Red" CornerRadius="5">
                                        <StackPanel>                                                
                                            <TextBlock Margin="4" FontSize="15" Text="{TemplateBinding Content}"/>
                                            <TextBlock>I am a header</TextBlock>
                                        </StackPanel>
                                        </Border>
                                    </ContentPresenter.Content>                                        
                                </ContentPresenter>
                            </Border>
                            <DataTemplate.Triggers>
                                <DataTrigger Binding="{Binding RelativeSource={RelativeSource Mode=FindAncestor,AncestorType={x:Type TabItem}},Path=IsSelected}" Value="True">
                                    <Setter TargetName="grid" Property="Background" Value="Green"/>
                                </DataTrigger>
                            </DataTemplate.Triggers>
                        </DataTemplate>
                    </Setter.Value>
                </Setter>
            </Style>
        </TabControl.Resources>            
        <TabItem Header="Hey1" Name="tabItem1">
        </TabItem>
        <TabItem Header="Hey2" Name="tabItem2">
        </TabItem>
        <TabItem Header="Hey3" Name="tabItem3">
        </TabItem>
        <TabItem Header="Hey4" Name="tabItem4">
        </TabItem>
    </TabControl>