编程语言
首页 > 编程语言> > c# – 在Mahapps.Metro中设置WindowButtonCommands样式

c# – 在Mahapps.Metro中设置WindowButtonCommands样式

作者:互联网

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

我正在使用Mahapps.Metro并且我已经成功地实现了我想要的结果,但只使用了MetroWindow类中过时的WindowMinButtonStyle,WindowMaxButtonStyle和WindowCloseButtonStyle属性.例如WindowMinButtonStyle属性上的过时消息读取:

This property will be deleted in the next release. You should use LightMinButtonStyle or DarkMinButtonStyle in WindowButtonCommands to override the style.

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

编辑 – 这是产生警告的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>

我还应该提到我正在使用Mahapps.Metro的新v1.2.0,但我在之前的版本中遇到了同样的问题.
具有过时属性的Mahapps.Metro源代码:https://github.com/MahApps/MahApps.Metro/blob/develop/MahApps.Metro/Controls/MetroWindow.cs#L88-L93

解决方法:

基于crumbl3d changes,简短如何…

现在有两种样式(Light,Dark)将根据OverrideDefaultWindowCommandsBrush属性(在MetroWindow中可用)使用,并且它的亮度(默认为Light样式).

所以,把这些放在你的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>

希望这可以帮助.

标签:c,titlebar,wpf,xaml,mahapps-metro
来源: https://codeday.me/bug/20190528/1168106.html