2

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...

Glorfindel
  • 21,988
  • 13
  • 81
  • 109
user606521
  • 14,486
  • 30
  • 113
  • 204

1 Answers1

1

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.

Rachel
  • 130,264
  • 66
  • 304
  • 490
  • 1
    Those sub-pages still do not supply the defaults but examples. I for one prefer to link to [this question](http://stackoverflow.com/questions/1559261/control-template-for-existing-controls-in-wpf). – H.B. Mar 21 '12 at 18:29
  • 1
    @H.B. Thanks, I'll try and start using that answer for default template questions instead of the MSDN examples. – Rachel Mar 21 '12 at 18:31
  • I agree with H.B. MSDN does not provide original templates, but some examples... – Kino101 Jan 02 '21 at 23:17