TabItem - 如何更改模板并保留默认样式?

时间:2012-03-20 15:39:58

标签: wpf tabcontrol tabitem

我有以下代码:

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

我想保留TabItem的默认样式 - 我的意思是填充/边距/ BorderBrush / BorderThickness等等...这就是我写BasedOn="..."的原因。 但它不起作用 - 我认为它将与TabItem呈现相同而没有任何自定义样式但它没有 - 它只是呈现一些文本(由ContentPresenter)。样式没有获取默认属性值...我怎么能这样做? 我需要ControlTemplate我的风格...

1 个答案:

答案 0 :(得分:2)

您正在覆盖TabItem.Template,这是告诉WPF如何绘制对象的原因

如果您要创建基于默认TabItem.Template的模板,您可以从MSDN获取default template并将其更改为您想要的任何内容。