2012-03-20 107 views
2

我有以下代码: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

回答

2

要覆盖TabItem.Template,这就是告诉WPF如何如果你想创建一个基于默认TabItem.Template模板拉拢的对象

,你可以从MSDN获得default template,并将其改为任何你想要的。

+1

这些子页面仍然不提供默认值,但提供示例。我为一个喜欢链接到[这个问题](http://stackoverflow.com/questions/1559261/control-template-for-existing-controls-in-wpf)。 – 2012-03-21 18:29:23

+1

@ H.B。谢谢,我会尝试开始使用该答案作为默认模板问题,而不是MSDN示例。 – Rachel 2012-03-21 18:31:24