How To Wpf TabItem Style HeaderTemplate Binding?

24,796

Solution 1

Try this Instead,

<TabControl x:Name="tabCtrlMain" ItemsSource="{Binding Items}" >
        <TabControl.Resources>
            <Style TargetType="TabItem">
                <Setter Property="Header" Value="{Binding FileName}" />
                <Setter Property="HeaderTemplate">
                    <Setter.Value>
                        <DataTemplate DataType="{x:Type TabItem}">
                            <Border x:Name="grid">
                                    <ContentPresenter>
                                        <ContentPresenter.Content>
                                            <TextBlock Text="{TemplateBinding Content}"/>
                                        </ContentPresenter.Content>
                                    </ContentPresenter>
                                </Border>
                        </DataTemplate>
                    </Setter.Value>
                </Setter>
            </Style>
        </TabControl.Resources>
    </TabControl>

Solution 2

I know this is awfully old now, but I thought I'd throw my two cents in just for the sake of completeness and historical accuracy :)

I prefer to use the ItemContainerStyle to do the same thing just because it feels a little cleaner to me because it states the purpose exactly:

<TabControl ItemsSource="{Binding Items}">
  <TabControl.ItemContainerStyle>
    <Style TargetType="TabItem">
      <Setter Property="Header" Value="{Binding FileName}" />
      <Setter Property="HeaderTemplate">
        <Setter.Value>
          <DataTemplate>
            <Border>
              <TextBlock Text="{Binding Content}" />
            </Border>
          </DataTemplate>
        </Setter.Value>
      </Setter>
    </Style>
  </TabControl.ItemContainerStyle>
</TabControl>

Also, if the only goal is to get the FileName into the tabs then it can be much simpler:

<TabControl ItemsSource="{Binding Items}" DisplayMemberPath="FileName" />
Share:
24,796

Related videos on Youtube

Ali Yousefi
Author by

Ali Yousefi

My SignalGo project: https://github.com/SignalGo/SignalGo-full-net My Email: [email protected]

Updated on December 06, 2020

Comments

  • Ali Yousefi
    Ali Yousefi over 3 years

    How To do Wpf TabItem Style HeaderTemplate Binding?

    Code:

    <TabControl x:Name="tabCtrlMain" ItemsSource="{Binding Items}" >
            <TabControl.Resources>
                <Style TargetType="TabItem">
                    <Setter Property="HeaderTemplate">
                        <Setter.Value>
                            <DataTemplate DataType="{x:Type TabItem}">
                                <TextBlock Text="{Binding FileName}"/>
                            </DataTemplate>
                        </Setter.Value>
                    </Setter>
                </Style>
            </TabControl.Resources>
        </TabControl>
    

    this code is not working when binding:

    <TextBlock Text="{Binding FileName}"/>
    
  • MojoFilter
    MojoFilter about 11 years
    The biggest problem here is that you can't use a TemplateBinding inside a DataTemplate. Other than that, it should do what was asked.