简体   繁体   English

如何更改 MahApps TabItem 的文本颜色

[英]How to change text color of a MahApps TabItem

I use MahApps in a WPF application and I have serveral TabControl s.我在 WPF 应用程序中使用 MahApps 并且我有几个TabControl

I found how to change font size with:我找到了如何更改字体大小:

<TabControl.Resources>
    <Style TargetType="{x:Type TabItem}" BasedOn="{StaticResource {x:Type TabItem}}">
       <Setter Property="controls:HeaderedControlHelper.HeaderFontSize" Value="12" />
    </Style>
</TabControl.Resources>

I would like to modify the text color, too, but I don't find how to do it.我也想修改文本颜色,但我不知道该怎么做。

Could anyone help me please?有人可以帮我吗?

Unfortunately, neither HeaderedControlHelper nor TabControlHelper can be used to set the header brushes, as they are hard-coded in the control template of the default style for TabItem .不幸的是, HeaderedControlHelperTabControlHelper都不能用于设置 header 画笔,因为它们在TabItem默认样式的控件模板中是硬编码的。

In order to change the header brushes, you have to copy the default style for TabItem from GitHub and adapt the Foreground brushes which determine the text color in all control states that you need.为了更改 header 画笔,您必须从 GitHub 复制TabItem的默认样式并调整Foreground画笔,以确定您需要的所有控制状态中的文本颜色。 There are five sections where the foreground brushes are set.有五个部分设置了前景画笔。

  1. Default foreground if not overridden by any other state.如果没有被任何其他 state 覆盖,则默认前景。

     <Setter Property="Foreground" Value="{DynamicResource MahApps.Brushes.Text}" />
  2. Foreground brush in the selected state of a tab.前景画笔在state的一个选项卡中选中。

     <Trigger Property="IsSelected" Value="true"> <Setter TargetName="ContentSite" Property="TextElement.Foreground" Value="{DynamicResource MahApps.Brushes.Accent}" /> <Setter TargetName="Underline" Property="BorderBrush" Value="{Binding RelativeSource={RelativeSource TemplatedParent}, Path=(mah:TabControlHelper.UnderlineSelectedBrush), Mode=OneWay}" /> </Trigger>
  3. Foreground brush when a tab is not selected.未选择选项卡时的前景画笔。

     <Trigger Property="IsSelected" Value="false"> <Setter TargetName="ContentSite" Property="TextElement.Foreground" Value="{DynamicResource MahApps.Brushes.Gray}" /> </Trigger>
  4. Foreground brush when the mouse is over the tab header.当鼠标悬停在标签 header 上时,前景画笔。

     <Trigger SourceName="Border" Property="IsMouseOver" Value="True"> <Setter TargetName="ContentSite" Property="TextElement.Foreground" Value="{DynamicResource MahApps.Brushes.Gray.MouseOver}" /> <Setter TargetName="Underline" Property="BorderBrush" Value="{Binding RelativeSource={RelativeSource TemplatedParent}, Path=(mah:TabControlHelper.UnderlineMouseOverBrush), Mode=OneWay}" /> </Trigger>
  5. Foreground brush when the mouse is over the tab header but not selected.当鼠标悬停在 header 选项卡上但未选中时,前景画笔。

     <MultiTrigger> <MultiTrigger.Conditions> <Condition SourceName="Border" Property="IsMouseOver" Value="True" /> <Condition Property="IsSelected" Value="True" /> </MultiTrigger.Conditions> <Setter TargetName="ContentSite" Property="TextElement.Foreground" Value="{DynamicResource MahApps.Brushes.Highlight}" /> <Setter TargetName="Underline" Property="BorderBrush" Value="{Binding RelativeSource={RelativeSource TemplatedParent}, Path=(mah:TabControlHelper.UnderlineMouseOverSelectedBrush), Mode=OneWay}" /> </MultiTrigger>

Move your adapted style to a resource dictionary and reference the style by key explicitly or remove the x:Key to make it implicit so it will be applied to all tab items in scope automatically.将您调整的样式移动到资源字典并通过键显式引用样式或删除x:Key以使其隐式,因此它将自动应用于 scope 中的所有选项卡项。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM