gpt4 book ai didi

c# - 地铁(MahApps)WPF : Override TabItem style

转载 作者:行者123 更新时间:2023-12-04 17:51:12 27 4
gpt4 key购买 nike

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

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

(绑定(bind)是通过 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" />

选项卡控件/项目样式:

<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中的样式

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

关于c# - 地铁(MahApps)WPF : Override TabItem style,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44712231/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com