WPF MetroWindow Min Max 应用程序失去焦点时关闭背景

WPF MetroWindow Min Max Close background when app loses focus

我正在开发一个使用 MahApps MetroWindow 作为主窗口的 WPF 应用程序。我在 MetroWindow 中构建的最小化/最大化和关闭按钮上使用了样式。目前样式如下:(最大化和关闭样式类似)

<Controls:MetroWindow.WindowMinButtonStyle>
        <Style TargetType="Button">
            <Setter Property="Background" Value="{DynamicResource PrimaryHueMidBrush}"/>
            <Setter Property="Foreground" Value="{DynamicResource BlackBrush}"/>
            <Setter Property="Width" Value="35"/>
            <Setter Property="HorizontalContentAlignment"
                    Value="Center" />
            <Setter Property="VerticalContentAlignment"
                    Value="Center" />
            <Setter Property="Padding"
                    Value="1" />
            <Setter Property="Template">
                <Setter.Value>
                    <ControlTemplate TargetType="{x:Type Button}">
                        <Grid x:Name="grid"
                              Background="{TemplateBinding Background}">
                            <ContentPresenter x:Name="contentPresenter"
                                              HorizontalAlignment="{TemplateBinding HorizontalContentAlignment}"
                                              Margin="{TemplateBinding Padding}"
                                              VerticalAlignment="{TemplateBinding VerticalContentAlignment}"
                                              SnapsToDevicePixels="{TemplateBinding SnapsToDevicePixels}"
                                              RecognizesAccessKey="True"
                                              Opacity="0.75" />
                        </Grid>
                        <ControlTemplate.Triggers>
                            <Trigger Property="IsMouseOver"
                                     Value="True">
                                <Setter TargetName="contentPresenter"
                                        Property="Opacity"
                                        Value="1" />
                            </Trigger>
                            <Trigger Property="IsMouseOver"
                                     Value="False">
                                <Setter TargetName="contentPresenter"
                                        Property="Opacity"
                                        Value=".5" />
                            </Trigger>
                            <Trigger Property="IsEnabled"
                                     Value="false">
                                <Setter Property="Foreground"
                                        Value="#ADADAD" />
                            </Trigger>
                        </ControlTemplate.Triggers>
                    </ControlTemplate>
                </Setter.Value>
            </Setter>
        </Style>
    </Controls:MetroWindow.WindowMinButtonStyle>

样式本身按预期工作,但我错过了一件事:

虽然我可以更改此事件中按钮的背景,但我在 MetroWindow 中找不到最小最大关闭按钮。

private void Window_Deactivated(object sender, EventArgs e)
        {
            SolidColorBrush gray = new SolidColorBrush(Colors.Gray);
            StatusBar.Background = gray;
        }

        private void Window_Activated(object sender, EventArgs e)
        {
            StatusBar.Background = (SolidColorBrush)FindResource("PrimaryHueMidBrush");
        }

有没有办法在应用程序失去焦点时设置这些按钮的背景 (window_deactivated)?

只需将 DataTrigger 添加到 Style 中绑定到父 window 的 Active 属性 的 <ControlTemplate.Triggers> 元素:

<DataTrigger Binding="{Binding IsActive, RelativeSource={RelativeSource AncestorType=Window}}"
             Value="False">
    <Setter Property="Background" Value="Gray"/>
</DataTrigger>