Reputation: 331
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. 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
<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
<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
Upvotes: 3
Views: 1235
Reputation: 13188
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>
Upvotes: 1