简体   繁体   English

在Mahapps.Metro中设置WindowButtonCommands样式

[英]Setting WindowButtonCommands styles in Mahapps.Metro

I want to change the style of the Min, Max and Close buttons for my WPF application. 我想更改WPF应用程序的Min,Max和Close按钮的样式。

I'm using Mahapps.Metro and I have successfully managed to achieve the result I want, but only with the obsolete WindowMinButtonStyle , WindowMaxButtonStyle and WindowCloseButtonStyle properties in the MetroWindow class. 我正在使用Mahapps.Metro并且我已经成功地实现了我想要的结果,但只使用了MetroWindow类中过时的WindowMinButtonStyleWindowMaxButtonStyleWindowCloseButtonStyle属性。 The obsolete message on for example the WindowMinButtonStyle property reads: 例如WindowMinButtonStyle属性上的过时消息读取:

This property will be deleted in the next release. 此属性将在下一版本中删除。 You should use LightMinButtonStyle or DarkMinButtonStyle in WindowButtonCommands to override the style. 您应该在WindowButtonCommands中使用LightMinButtonStyle或DarkMinButtonStyle来覆盖样式。

The problem is that I can't figure out how specifically to do that. 问题是我无法弄清楚具体如何做到这一点。 The MetroWindow class has a field called WindowButtonCommands , but it is internal , so that seems to be the wrong tree to bark up. MetroWindow类有一个名为WindowButtonCommands的字段,但它是internal ,所以这似乎是错误的树。 I'm fairly new to WPF and there is no info on how to do this in the guides on their website, so I'm pretty lost. 我是WPF的新手,并且没有关于如何在他们网站的指南中做到这一点的信息,所以我很丢失。 I'm hoping someone can provide me with a short code example to point me in the right direction. 我希望有人可以给我一个简短的代码示例,指出我正确的方向。

EDIT - Here is XAML that produces the warning: 编辑 - 这是产生警告的XAML:

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

I should also mention I'm using the new v1.2.0 of Mahapps.Metro, but I had the same issue with the previous version. 我还应该提到我正在使用Mahapps.Metro的新v1.2.0,但我在之前的版本中遇到了同样的问题。 Mahapps.Metro source code that has the Obsolete attributes: https://github.com/MahApps/MahApps.Metro/blob/develop/MahApps.Metro/Controls/MetroWindow.cs#L88-L93 具有Obsolete属性的Mahapps.Metro源代码: https//github.com/MahApps/MahApps.Metro/blob/develop/MahApps.Metro/Controls/MetroWindow.cs#L88-L93

based on crumbl3d changes , a short how to... 基于crumbl3d的 变化 ,简短如何...

There are now two styles (Light, Dark) which will be used based on OverrideDefaultWindowCommandsBrush property (available at MetroWindow ) and it's lightness (default is the Light style). 现在有两种样式(Light,Dark)将根据OverrideDefaultWindowCommandsBrush属性(在MetroWindow可用)使用,并且它的亮度(默认为Light样式)。

So, put these at your App.xaml (or something else) 所以,把这些放在你的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>

Edit 编辑

If you only want to use this at one window then you can create a style with a key and use it at this window like this: 如果您只想在一个窗口中使用它,那么您可以使用键创建一个样式并在此窗口中使用它,如下所示:

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

The style located in App.xaml 位于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>

Hope this helps. 希望这可以帮助。

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

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