关于c#:Metro(MahApps)WPF:重写TabItem样式

Metro (MahApps) WPF: Override TabItem style

我喜欢WPF的MahApps Metro的很多东西,还有一些我不喜欢的东西。其中之一是"地铁"标签样式,因此我正在尝试对其进行调整。还是写我自己的,真的。

我的问题是,尽管我可以创建选项卡控件样式,但似乎根本无法覆盖TabItem样式。我开始很简单:无论内容是什么,都是红色的静态文本。但是我什至无法做到这一点。它一直使用Metro风格。

(绑定通过Caliburn.Micro。)

ShellView.xaml:

1
2
3
4
5
6
7
8
<TabControl Style="{StaticResource TabControlStyle}" DockPanel.Dock="Top"
    x:Name="AvailableTabs">
    <TabControl.ItemTemplate>
        <DataTemplate>
            <TextBlock Text="{Binding DisplayName}" />
        </DataTemplate>
    </TabControl.ItemTemplate>
</TabControl>

内部App.xaml:

1
2
3
4
5
6
7
8
9
<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 /项目样式:

1
2
3
4
5
6
7
8
9
10
<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

TabControl的样式肯定有效-选项卡和内容之间的水平线的存在足以证明这一点。我还尝试了使用{DynamicResource WhiteBrush}(来自mahapps)这种样式,以查看是否无法找到mahapps资源,但是效果很好。但是选项卡本身仍在使用Metro风格。

我究竟做错了什么?这让我发疯,我无法摆脱自己在做既简单又愚蠢的事情的感觉。 :)

(此外,我尝试将BasedOn={StaticResource MetroTabItem}添加到TabItem样式中,但这也不起作用。)


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

Resources/CustomControls.xaml中的样式

1
2
3
4
5
6
7
8
9
10
11
<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样式阴影。 应该是相反的方式:

应用程式

1
2
3
4
5
6
7
8
9
10
11
<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>