简体   繁体   English

为我覆盖 Mahapps Metro 风格 header Tabitem

[英]Overwrite Mahapps Metro style for me header Tabitem

I am working with WPF and MVVM.我正在使用 WPF 和 MVVM。 I installed Mahapps Metro, this nuget package provides all styles for my app.我安装了 Mahapps Metro,这个 nuget package 为我的应用程序提供了所有 styles。

I made a TabControl, but the FontSize that Mahapps uses for the header in each TabItem is very big for my application.我制作了一个 TabControl,但 Mahapps 用于每个 TabItem 中的 header 的 FontSize 对于我的应用程序来说非常大。

I need to create a StaticResource that changes the FontSize of the header in a TabItem without removing others properties that Mahapps provides.我需要创建一个 StaticResource 来更改 TabItem 中 header 的 FontSize 而不删除 Mahapps 提供的其他属性。

Put the following code in your window's resources like: 将以下代码放在窗口的资源中:

<Window
......
xmlns:mah="http://metro.mahapps.com/winfx/xaml/controls"
......
>
<Window.Resources>
        <Style x:Key="MenuLevel2" BasedOn="{StaticResource MetroTabItem}" TargetType="{x:Type TabItem}">
            <Setter Property="mah:ControlsHelper.HeaderFontSize" Value="24"></Setter>
        </Style>
<Window.Resources>

Looking at the source code [ https://github.com/MahApps/MahApps.Metro/blob/master/MahApps.Metro/Styles/Controls.TabControl.xaml ,at line 158] you can see that the programmers made a special exception to set this property, because the font is in a Control Template, inside a Content Presenter. 查看源代码[ https://github.com/MahApps/MahApps.Metro/blob/master/MahApps.Metro/Styles/Controls.TabControl.xaml ,第158行],您可以看到程序员做了一个特殊的例外设置此属性,因为该字体位于Content Presenter内的控件模板中。 It is easier to set ControlsHelper.HeaderFontSize property. 设置ControlsHelper.HeaderFontSize属性更容易。

As suggested in the above answer, put the below code in Window.Resources 如上面的答案所示,将下面的代码放在Window.Resources中

    <Window
......
xmlns:mah="http://metro.mahapps.com/winfx/xaml/controls"
......
>
<Window.Resources>
        <Style x:Key="MenuLevel2" BasedOn="{StaticResource MetroTabItem}" TargetType="{x:Type TabItem}">
            <Setter Property="mah:ControlsHelper.HeaderFontSize" Value="15"></Setter>
        </Style>
<Window.Resources>

In the TabItem section add the style details. 在TabItem部分中添加样式详细信息。

<TabItem Header="Dimension Alias" Style="{DynamicResource MenuLevel2}">

This worked for me. 这对我有用。

It looks like the API changed since 2019. The 'ControlHelper' doesn't seem to exist anymore.看起来 API 自 2019 年以来发生了变化。“ControlHelper”似乎不再存在。 For some info on the changes see: link to MahApps GitHub issue有关更改的一些信息,请参阅:链接到 MahApps GitHub 问题

Here is the code that works for me:这是对我有用的代码:

<mah:MetroWindow.Resources>
        <Style BasedOn="{StaticResource MahApps.Styles.TabControl}" TargetType="TabControl">
            <Setter Property="mah:HeaderedControlHelper.HeaderFontSize" Value="12" />
            <Setter Property="mah:HeaderedControlHelper.HeaderFontWeight" Value="Bold" />                 
        </Style>
</mah:MetroWindow.Resources>

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

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