2011-05-16 59 views
1

我有一个TabControl与自定义TabItem。视图xaml看起来像这样。WPF自定义TabItem在restyle上消失了吗?

<UserControl x:Class="PeripheryModule.Views.MainTabView" 
      xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
      xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 
      xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
      xmlns:d="http://schemas.microsoft.com/expression/blend/2008" 
      xmlns:local="clr-namespace:PeripheryModule.Controls" 
      mc:Ignorable="d" 
      d:DesignHeight="300" d:DesignWidth="300"> 
    <Grid> 
     <TabControl> 

      <TabItem Header="TabItem 4" /> 
      <local:CustomTabItem Header="Testing" /> 

     </TabControl> 
    </Grid> 
</UserControl> 

的CustomTabItem.xaml文件看起来像这样

<ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 
        xmlns:local="clr-namespace:PeripheryModule.Controls"> 

    <Style TargetType="{x:Type local:CustomTabItem}"> 
     <Setter Property="Template"> 
      <Setter.Value> 
       <ControlTemplate TargetType="{x:Type local:CustomTabItem}"> 
        <Border Background="{TemplateBinding Background}" 
          BorderBrush="{TemplateBinding BorderBrush}" 
          BorderThickness="{TemplateBinding BorderThickness}"> 
        </Border> 
       </ControlTemplate> 
      </Setter.Value> 
     </Setter> 
    </Style> 

</ResourceDictionary> 

和CustomTabItem.xaml.cs文件看起来像这样

using System; 
using System.Collections.Generic; 
using System.Linq; 
using System.Text; 
using System.Windows; 
using System.Windows.Controls; 
using System.Windows.Data; 
using System.Windows.Documents; 
using System.Windows.Input; 
using System.Windows.Media; 
using System.Windows.Media.Imaging; 
using System.Windows.Navigation; 
using System.Windows.Shapes; 

namespace PeripheryModule.Controls 
{ 
    public class CustomTabItem : TabItem 
    { 
     static CustomTabItem() 
     { 
      DefaultStyleKeyProperty.OverrideMetadata(typeof(CustomTabItem), new FrameworkPropertyMetadata(typeof(CustomTabItem))); 
     } 
    } 
} 

我没有得到任何引发的错误,所发生的只是tabItem根本不显示。但是,如果我将CustomTabItem.aspx.cs中的DefaultStyleKeyProperty行注释掉,它会显示出来。

无论如何,我可能已经设置了错误。最终目标是拥有可关闭的标签。

回答

1

摘录:

首先,如果你不熟悉的主题风格VS风格明确的概念,我建议你阅读这篇博客http://www.interact-sw.co.uk/iangblog/2007/02/14/wpfdefaulttemplate。所有控件都需要一个主题样式(通常在名为Themes的文件夹下名为Aero.NormalColor.xaml/Generic.xaml/etc的文件中定义),它定义了它们的默认外观(Template)/属性和可选的显式样式在元素/窗口/应用程序级别使用隐式键或显式键)。

DefaultStyleKeyProperty定义了用于查找控件主题风格的关键字。如果您注释掉该行,则最终将使用基类的默认主题样式。作为一个快速测试,将自定义控件的基类更改为Button。如果您注释掉该行,您的自定义控件将获得Button的主题风格,并且它看起来像一个按钮。如果您不注释该行,则您的自定义控件将获得generic.xaml中定义的默认样式。

来源:http://social.msdn.microsoft.com/Forums/en-US/wpf/thread/9df46c62-3715-4e16-8ef6-538068c28eb6/

编辑:

是否有任何理由,你为什么不想定义页眉模板和分配呢?

ex。 <TabItem Header="Testing" HeaderTemplate="{StaticResource customHeaderTemplate}" />

0

正如Schalk指出的那样,DefaultStyleKeyProperty标识了默认Style的键,它必须包含在Themes文件夹下的Resource字典中。因此,如果您将CustomTabItem.xaml移动/重命名为Themes\Generic.xaml,那么它应该加载并应用。

通过将名为AeroNormalColor.xaml,Classic.xaml等的文件添加到Themes文件夹中,您还可以基于系统主题创建不同版本的样式。这些文件也需要有你的风格的副本,大概是视觉上的调整。

您可以从here下载本地控件的默认样式。这将允许您复制TabItem的默认样式并将其调整为您的需要。

或者你可以在这个question中做些什么,其中关闭按钮被添加到HeaderTemplate。