简体   繁体   English

如何设置MahApps Window Grid的样式?

[英]How To Style MahApps Window Grid?

I'm using Mahapps UI toolkit for my WPF application, and I need my grid which is placed inside MetroWindow to change its color when application is not active as MetroWindow does. 我正在为我的WPF应用程序使用Mahapps UI工具包,我需要放置在MetroWindow内部的网格,以便在应用程序不活动时更改其颜色,如MetroWindow所做的那样。 I've tried several solutions, but in vain: 我尝试了几种解决方案,但徒劳无功:

<Grid Background="{DynamicResource AccentColorBrush}" >
                <TextBlock Text="Test" Foreground="#FFFFFF"  VerticalAlignment="Center" Padding="10,0,0,0"/>
</Grid>

This solution either sets grid color as AccentColorBrush 此解决方案将网格颜色设置为AccentColorBrush

<Grid Background="{Binding RelativeSource={RelativeSource FindAncestor, AncestorType=controls:MetroWindow}, Path=WindowTitleBrush}" >
                <TextBlock Text="Test" Foreground="#FFFFFF"  VerticalAlignment="Center" Padding="10,0,0,0"/>
</Grid>

I can also set grid color as inactive with NonActiveWindowTitleBrush property 我还可以使用NonActiveWindowTitleBrush属性将网格颜色设置为非活动状态

<Grid Background="{Binding RelativeSource={RelativeSource FindAncestor, AncestorType=controls:MetroWindow}, Path=NonActiveWindowTitleBrush}" >
                    <TextBlock Text="Test" Foreground="#FFFFFF"  VerticalAlignment="Center" Padding="10,0,0,0"/>
</Grid>

I'll be thankful for any help. 我会感谢任何帮助。

Regards, Dmitry 此致,德米特里

Try this: 尝试这个:

<Controls:MetroWindow x:Class="MahApps.Metro.Application12.MainWindow"
                  xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
                  xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
                  xmlns:Controls="http://metro.mahapps.com/winfx/xaml/controls"
                  xmlns:Custom="http://metro.mahapps.com/winfx/xaml/shared" 
                  Title="MainWindow"
                  Height="350" 
                  Width="525">

<Controls:MetroWindow.Resources>

    <Style x:Key="GridStyle1" TargetType="{x:Type Grid}">
        <Setter Property="Background" Value="{StaticResource AccentColorBrush}"/>
        <Style.Triggers>
            <DataTrigger Binding="{Binding IsActive, RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type Controls:MetroWindow}}}" Value="False">
                <Setter Property="Background"  Value="{StaticResource GrayBrush3}"/>
            </DataTrigger>
        </Style.Triggers>
    </Style>

</Controls:MetroWindow.Resources>

<Grid x:Name="grid1" Margin="50" HorizontalAlignment="Center" VerticalAlignment="Center" Height="232" Width="409" Style="{StaticResource GridStyle1}">

    <TextBlock HorizontalAlignment="Center" VerticalAlignment="Center" FontSize="32" Text="{Binding IsActive, RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type Controls:MetroWindow}}}"/>

</Grid>

在此输入图像描述

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

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