Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Setting WindowButtonCommands styles in Mahapps.Metro

I want to change the style of the Min, Max and Close buttons for my WPF application.

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. The obsolete message on for example the WindowMinButtonStyle property reads:

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

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

<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 source code that has the Obsolete attributes: https://github.com/MahApps/MahApps.Metro/blob/develop/MahApps.Metro/Controls/MetroWindow.cs#L88-L93

like image 712
Jan Ivar Z. Carlsen Avatar asked Jan 07 '23 21:01

Jan Ivar Z. Carlsen


1 Answers

based on crumbl3d changes, a short how to...

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

So, put these at your App.xaml (or something else)

<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

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

like image 148
punker76 Avatar answered Jan 10 '23 10:01

punker76