Search code examples
wpftabcontroltabitem

TabItem - how to change template and leave default style?


I have the following code:

<Window x:Class="kkk.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        Title="MainWindow" Height="350" Width="525">
    <Window.Resources>
        <Style x:Key="tabitemstyle" TargetType="{x:Type TabItem}" BasedOn="{StaticResource {x:Type TabItem}}">
            <Setter Property="Template">
                <Setter.Value>
                    <ControlTemplate TargetType="{x:Type TabItem}">
                        <Grid>
                            <Border x:Name="Bd" BorderBrush="{TemplateBinding BorderBrush}" BorderThickness="{TemplateBinding BorderThickness}">
                                <ContentPresenter x:Name="ContentSite" ContentSource="Header"></ContentPresenter>
                            </Border>
                        </Grid>
                    </ControlTemplate>
                </Setter.Value>
            </Setter>
        </Style>
    </Window.Resources>
    <Grid>

        <TabControl>
            <TabItem Header="tab1" Style="{StaticResource tabitemstyle}"></TabItem>
            <TabItem Header="tab1" Style="{StaticResource tabitemstyle}"></TabItem>
        </TabControl>

    </Grid>
</Window>

I want to preserve the default style of TabItem - I mean the padding/margins/BorderBrush/BorderThickness and so on... That is why I wrote BasedOn="...". But it doesn't work - I thought it will render same as TabItem without any custom style but it doesn't - it just renders some text (by the ContentPresenter). The style doesn't get default properties values... How I can do it? And I need the ControlTemplate inside my style...


Solution

  • You are overwriting the TabItem.Template, which is what tells WPF how to draw the object

    If you want to create a template that is based on the default TabItem.Template, you can get the default template from MSDN and alter it to whatever you want.