在Mahapps.Metro中设置WindowButtonCommands样式

时间:2016-01-02 06:07:36

标签: c# wpf xaml titlebar mahapps.metro

我想更改WPF应用程序的Min,Max和Close按钮的样式。

我正在使用Mahapps.Metro并且我已成功设法达到我想要的结果,但仅限WindowMinButtonStyleWindowMaxButtonStyleWindowCloseButtonStyle属性{ {1}}课程。例如MetroWindow属性上的过时消息显示为:

  

此属性将在下一版本中删除。你应该在WindowButtonCommands中使用LightMinButtonStyle或DarkMinButtonStyle来覆盖样式。

问题在于我无法弄清楚具体如何做到这一点。 WindowMinButtonStyle类有一个名为MetroWindow的字段,但它是WindowButtonCommands,所以这似乎是错误的树。我是WPF的新手,并且没有关于如何在他们网站的指南中做到这一点的信息,所以我很丢失。我希望有人可以给我一个简短的代码示例,指出我正确的方向。

编辑 - 这是产生警告的XAML:

internal

我还应该提到我正在使用Mahapps.Metro的新v1.2.0,但我在之前的版本中遇到了同样的问题。 具有<controls:MetroWindow x:Class="Project.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" WindowMinButtonStyle="{DynamicResource DarkWindowButtonStyle}" WindowMaxButtonStyle="{DynamicResource DarkWindowButtonStyle}" WindowCloseButtonStyle="{DynamicResource DarkWindowCloseButtonStyle}"> <Grid> </Grid> </controls:MetroWindow> 属性的Mahapps.Metro源代码:https://github.com/MahApps/MahApps.Metro/blob/develop/MahApps.Metro/Controls/MetroWindow.cs#L88-L93

1 个答案:

答案 0 :(得分:7)

基于crumbl3d changes,简短介绍如何...

现在有两种样式(Light,Dark)将根据OverrideDefaultWindowCommandsBrush属性(MetroWindow处可用)和它的亮度(默认为Light样式)使用。 / p>

所以,把这些放在你的App.xaml(或别的东西)

<Style x:Key="CustomLightMetroWindowButtonStyle" TargetType="{x:Type Button}" BasedOn="{StaticResource LightMetroWindowButtonStyle}">
    <Setter Property="Foreground" Value="Chocolate" />
</Style>

<Style x:Key="CustomDarkMetroWindowButtonStyle" TargetType="{x:Type Button}" BasedOn="{StaticResource DarkMetroWindowButtonStyle}">
    <Setter Property="Foreground" Value="Crimson" />
</Style>

<Style TargetType="{x:Type controls:WindowButtonCommands}" BasedOn="{StaticResource {x:Type controls:WindowButtonCommands}}">
    <Setter Property="LightMinButtonStyle" Value="{StaticResource CustomLightMetroWindowButtonStyle}" />
    <Setter Property="LightMaxButtonStyle" Value="{StaticResource CustomLightMetroWindowButtonStyle}" />
    <Setter Property="LightCloseButtonStyle" Value="{StaticResource CustomLightMetroWindowButtonStyle}" />
    <Setter Property="DarkMinButtonStyle" Value="{StaticResource CustomDarkMetroWindowButtonStyle}" />
    <Setter Property="DarkMaxButtonStyle" Value="{StaticResource CustomDarkMetroWindowButtonStyle}" />
    <Setter Property="DarkCloseButtonStyle" Value="{StaticResource CustomDarkMetroWindowButtonStyle}" />
</Style>

修改

如果您只想在一个窗口中使用它,那么您可以使用键创建一个样式并在此窗口中使用它,如下所示:

<controls:MetroWindow.WindowButtonCommands>
    <controls:WindowButtonCommands Style="{DynamicResource CustomWindowButtonCommandsStyleLocatedtInAppXaml}" />
</controls:MetroWindow.WindowButtonCommands>

App.xaml中的样式

<Style x:Key="CustomWindowButtonCommandsStyleLocatedtInAppXaml" TargetType="{x:Type controls:WindowButtonCommands}" BasedOn="{StaticResource {x:Type controls:WindowButtonCommands}}">
    <Setter Property="LightMinButtonStyle" Value="{StaticResource CustomLightMetroWindowButtonStyle}" />
    <Setter Property="LightMaxButtonStyle" Value="{StaticResource CustomLightMetroWindowButtonStyle}" />
    <Setter Property="LightCloseButtonStyle" Value="{StaticResource CustomLightMetroWindowButtonStyle}" />
    <Setter Property="DarkMinButtonStyle" Value="{StaticResource CustomDarkMetroWindowButtonStyle}" />
    <Setter Property="DarkMaxButtonStyle" Value="{StaticResource CustomDarkMetroWindowButtonStyle}" />
    <Setter Property="DarkCloseButtonStyle" Value="{StaticResource CustomDarkMetroWindowButtonStyle}" />
</Style>

希望这有帮助。