如何设置MahApps Window Grid的样式?

时间:2016-06-06 13:45:30

标签: c# wpf xaml data-binding mahapps.metro

我在我的WPF应用程序中使用Mahapps UI工具包,我需要放置在MetroWindow内部的网格,以便在应用程序不活动时更改其颜色,如MetroWindow所做的那样。我尝试了几种解决方案,但徒劳无功:

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

此解决方案将网格颜色设置为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>

我还可以使用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>

我会感谢任何帮助。

问候,德米特里

1 个答案:

答案 0 :(得分:1)

试试这个:

<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>

enter image description here