Customizing WindowButtonCommands styles in Mahapps.Metro

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

I use Mahapps.Metro , and I managed to achieve the result that I want, but only with outdated WindowMinButtonStyle WindowMaxButtonStyleand WindowCloseButtonStylein class MetroWindow. An outdated message, such as a property WindowMinButtonStyle, reads:

This property will be removed in a future version. You must use LightMinButtonStyle or DarkMinButtonStyle in WindowButtonCommands to override the style.

The problem is that I cannot figure out how to do this specifically. The class MetroWindowhas a field called WindowButtonCommands, but it is internal, therefore, it seems that it is the wrong tree to bark up. I am new to WPF and there is no information on how to do this in the manuals on their website, so I'm pretty lost. I hope someone can provide me with a short code example to point me in the right direction.

EDIT . Here XAML issues a 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 that I am using the new v1.2.0 from Mahapps.Metro, but I had the same problem with the previous version. Source code of Mahapps.Metro, which has attributes Obsolete: https://github.com/MahApps/MahApps.Metro/blob/develop/MahApps.Metro/Controls/MetroWindow.cs#L88-L93

+4
1

crumbl3d , ...

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

Edit

, :

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

, .

+4

Source: https://habr.com/ru/post/1622500/


All Articles