向 MetroWindow 标题栏内的图标添加自定义样式

Adding custom styles to icons inside Title bar of MetroWindow

我正在使用 MahApps.Metro in my WPF app. The default behavior of the title bar of its MetroWindow 除非您将鼠标悬停在图标上,否则其中的任何图标都具有低不透明度(变暗)显示。即使您不将鼠标悬停在图标上,我也希望它看起来与您将鼠标悬停在图标上时一样。我们怎样才能做到这一点?

鼠标悬停在 CupCakes 图标上[我希望即使没有鼠标悬停它看起来也一样]:

没有鼠标悬停:

MetroWindow.xaml:

<mah:MetroWindow x:Class="WPF_Mah_Metro_Test.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
        xmlns:mah="clr-namespace:MahApps.Metro.Controls;assembly=MahApps.Metro"
        xmlns:iconPacks="http://metro.mahapps.com/winfx/xaml/iconpacks"
        xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
        xmlns:local="clr-namespace:WPF_Mah_Metro_Test"
        mc:Ignorable="d"
        GlowBrush="{DynamicResource MahApps.Brushes.Accent}"
        ResizeMode="CanResizeWithGrip"
        WindowStartupLocation="CenterScreen"
        Height="450" Width="800">

    <mah:MetroWindow.LeftWindowCommands>
        <mah:WindowCommands>
            <Button x:Name="btnTest" Click="btnTest_Click"  ToolTip="Open up the GitHub site">
                <iconPacks:PackIconModern Width="22" Height="22" Kind="SocialGithubOctocat" />
            </Button>
        </mah:WindowCommands>
    </mah:MetroWindow.LeftWindowCommands>

    <mah:MetroWindow.RightWindowCommands>
        <mah:WindowCommands>
            <Button x:Name="btnTest1" Click="btnTest1_Click"  Content="Deploy CupCakes">
                <Button.ContentTemplate>
                    <DataTemplate>
                        <StackPanel Orientation="Horizontal">
                            <iconPacks:PackIconModern Width="22" Height="22" VerticalAlignment="Center" Kind="FoodCupcake" />
                            <TextBlock Margin="4 0 0 0" VerticalAlignment="Center" Text="{Binding}" />
                        </StackPanel>
                    </DataTemplate>
                </Button.ContentTemplate>
            </Button>
        </mah:WindowCommands>
    </mah:MetroWindow.RightWindowCommands>
    <Grid>
    </Grid>

</mah:MetroWindow>

要覆盖 WindowCommands 内部控件的样式,您必须为其创建自己的样式:

<Style x:Key="Styles.WindowCommands.Custom"
       BasedOn="{StaticResource MahApps.Styles.WindowCommands}"
       TargetType="{x:Type mah:WindowCommands}">
    <Style.Resources>
        <ResourceDictionary>
            <Style BasedOn="{StaticResource MahApps.Styles.Button.WindowCommands}" TargetType="{x:Type Button}">
                <Setter Property="Opacity" Value="1" />
            </Style>
            <Style BasedOn="{StaticResource MahApps.Styles.ToggleButton.WindowCommands}" TargetType="{x:Type ToggleButton}">
                <Setter Property="Opacity" Value="1" />
                <Style.Triggers>
                    <Trigger Property="IsChecked" Value="True">
                        <Setter Property="Opacity" Value="1" />
                    </Trigger>
                    <Trigger Property="IsMouseOver" Value="True">
                        <Setter Property="Opacity" Value="1" />
                    </Trigger>
                </Style.Triggers>
            </Style>
            <Style BasedOn="{StaticResource MahApps.Styles.SplitButton.WindowCommands}" TargetType="{x:Type mah:SplitButton}">
                <Setter Property="Opacity" Value="1" />
            </Style>
            <Style BasedOn="{StaticResource MahApps.Styles.DropDownButton.WindowCommands}" TargetType="{x:Type mah:DropDownButton}">
                <Setter Property="Opacity" Value="1" />
            </Style>
        </ResourceDictionary>
    </Style.Resources>
</Style>

现在,如果您将此密钥放入您的`App.xaml,您可以在您的应用程序中使用它,例如:

<mah:MetroWindow.LeftWindowCommands>
    <mah:WindowCommands Style="{StaticResource Styles.WindowCommands.Custom}">
    </mah:WindowCommands>
</mah:MetroWindow.LeftWindowCommands>

<mah:MetroWindow.RightWindowCommands>
    <mah:WindowCommands Style="{StaticResource Styles.WindowCommands.Custom}">
    </mah:WindowCommands>
</mah:MetroWindow.RightWindowCommands>

另一种解决方案是直接为按钮制作样式。

注意mah 的命名空间是xmlns:mah="http://metro.mahapps.com/winfx/xaml/controls"