如何设置 MahApps Window 网格的样式?
How To Style MahApps Window Grid?
我正在为我的 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>
如有任何帮助,我将不胜感激。
此致,德米特里
试试这个:
<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>
我正在为我的 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>
如有任何帮助,我将不胜感激。
此致,德米特里
试试这个:
<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>