Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to preserve mahapps metro styling when adding custom trigger?

I had a button that was styled in the default metro style. However when I added a trigger to the button, the style is overridden. How to preserve the original mahapps metro styling when adding your own style and triggers?

    <Button x:Name="startButton" Content="Start" HorizontalAlignment="Right"  VerticalAlignment="Bottom" Margin="0,0,20,26" Width="75" Click="startButton_Click" Height="67" Grid.Column="1">
        <!--<Button.Style>
            <Style TargetType="Button">
                <Style.Triggers>
                    <Trigger Property="IsMouseOver" Value="True">
                        <Setter Property="Foreground" Value="Red"></Setter>
                    </Trigger>
                </Style.Triggers>
            </Style>
        </Button.Style>-->
    </Button>

This is how the button looked originally

Before

This is how it looks after I added the trigger

After

like image 423
erotavlas Avatar asked Jan 24 '17 16:01

erotavlas


1 Answers

You need to set the "BasedOn" property of your style to the one mahapps.metro is providing as default:

<Style TargetType="Button" BasedOn="{StaticResource MetroButton}">

The documentation doesn't say the default static resource, but it's OpenSource so easy to track it down in the source code for the Controls.xaml you import to load the default styles in your app.xaml (or top of window etc):

https://github.com/MahApps/MahApps.Metro/tree/develop/src/MahApps.Metro/Styles

A search for TargetType="Button" finds our default style (without a key):

Which is even also based on the base style, MahApps.Metro.Styles.MetroButton.

like image 200
Joe Avatar answered Oct 23 '22 15:10

Joe