Metro (MahApps) WPF:覆盖 TabItem 样式

Metro (MahApps) WPF: Override TabItem style

WPF 版 MahApps Metro 有很多我喜欢的地方,但也有一些我不太喜欢的地方。其中之一是 Metro 选项卡样式,因此我正在尝试对其进行调整。或者自己写,真的。

我的问题是,虽然我可以创建选项卡控件样式,但我似乎根本无法覆盖 TabItem 样式。我从简单开始:只是红色的静态文本,不管那里有什么。但我什至无法做到这一点。它一直在使用 Metro 风格。

(绑定是通过Caliburn.Micro.)

ShellView.xaml:

<TabControl Style="{StaticResource TabControlStyle}" DockPanel.Dock="Top"
    x:Name="AvailableTabs">
    <TabControl.ItemTemplate>
        <DataTemplate>
            <TextBlock Text="{Binding DisplayName}" />
        </DataTemplate>
    </TabControl.ItemTemplate>
</TabControl>

里面App.xaml:

<ResourceDictionary Source="Resources/CustomControls.xaml" />

<!-- MahApps.Metro resource dictionaries. Make sure that all file names are Case Sensitive! -->
<ResourceDictionary Source="pack://application:,,,/MahApps.Metro;component/Styles/Controls.xaml" />
<ResourceDictionary Source="pack://application:,,,/MahApps.Metro;component/Styles/Fonts.xaml" />
<ResourceDictionary Source="pack://application:,,,/MahApps.Metro;component/Styles/Colors.xaml" />
<!-- Accent and AppTheme setting -->
<ResourceDictionary Source="pack://application:,,,/MahApps.Metro;component/Styles/Accents/Blue.xaml" />
<ResourceDictionary Source="pack://application:,,,/MahApps.Metro;component/Styles/Accents/BaseLight.xaml" />

TabControl/Item 样式:

<Style TargetType="TabItem" BasedOn="{StaticResource MetroTabItem}">
    <Setter Property="Background" Value="Red" />
    <Setter Property="Template">
        <Setter.Value>
            <ControlTemplate TargetType="TabItem">
                <TextBlock Text="Tab" />
            </ControlTemplate>
        </Setter.Value>
    </Setter>
</Style>

结果:

TabControl 的样式绝对有效——选项卡和内容之间存在的水平线就足以证明这一点。我还尝试在样式中使用 {DynamicResource WhiteBrush}(来自 mahapps)以查看它是否可能找不到 mahapps 资源,但效果很好。但是标签本身还是用的Metro风格。

我做错了什么?这快把我逼疯了,我无法摆脱自己正在做的事情既简单又愚蠢的感觉。 :)

(此外,我尝试将 BasedOn={StaticResource MetroTabItem} 添加到 TabItem 样式,但也没有用。)

这是您引用全局/隐式样式的顺序。

Resources/CustomControls.xaml

中的样式
<Style TargetType="TabItem" BasedOn="{StaticResource MetroTabItem}">
    <Setter Property="Background" Value="Red" />
    <Setter Property="Template">
        <Setter.Value>
            <ControlTemplate TargetType="TabItem">
                <TextBlock Text="Tab" Padding="10" Margin="1,0" 
                           Background="{TemplateBinding Background}" />
            </ControlTemplate>
        </Setter.Value>
    </Setter>
</Style>

MahApps 样式隐藏,因为它首先被引用。应该反过来:

App.xaml

<ResourceDictionary.MergedDictionaries>
    <!-- MahApps.Metro resource dictionaries. Make sure that all file names are Case Sensitive! -->
    <ResourceDictionary Source="pack://application:,,,/MahApps.Metro;component/Styles/Controls.xaml" />
    <ResourceDictionary Source="pack://application:,,,/MahApps.Metro;component/Styles/Fonts.xaml" />
    <ResourceDictionary Source="pack://application:,,,/MahApps.Metro;component/Styles/Colors.xaml" />
    <!-- Accent and AppTheme setting -->
    <ResourceDictionary Source="pack://application:,,,/MahApps.Metro;component/Styles/Accents/Blue.xaml" />
    <ResourceDictionary Source="pack://application:,,,/MahApps.Metro;component/Styles/Accents/BaseDark.xaml" />
    <!-- Last in line will prevail -->
    <ResourceDictionary Source="Resources/CustomControls.xaml" />
</ResourceDictionary.MergedDictionaries>