2

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)

Markus
  • 195
  • 1
  • 11
  • you want to change `Header` of `TabITem` or the `TabItem` itself? – StepUp May 18 '16 at 13:56
  • i want to change the header – Markus May 18 '16 at 14:12
  • I'm not sure why your code doesn't work, I think it should and [this answer](http://stackoverflow.com/a/5651542/302677) 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 May 18 '16 at 20:12

2 Answers2

0

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>
StepUp
  • 36,391
  • 15
  • 88
  • 148
  • @Markus feel free to ask any question. If you feel that my reply helps to you, then you can mark my reply as an answer to simplify future search of other people. Please, read this http://meta.stackexchange.com/questions/5234/how-does-accepting-an-answer-work – StepUp May 18 '16 at 15:03
  • i bind the Itemssource of the TabControl to an Observable collection. I am not sure how your approach helps me to get a dynamic size of tabs depending on the items in my List (your solution adds 1 TabItem...) – Markus May 18 '16 at 15:13
  • @Markus I've updated answer and it can be seen that this `TabControl` with a dynamic size. You can add as many as you want. – StepUp May 18 '16 at 18:10
0

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

Markus
  • 195
  • 1
  • 11