How to preserve mahapps metro styling when adding custom trigger?

☆樱花仙子☆ 提交于 2019-11-30 18:46:50

问题


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

This is how it looks after I added the trigger


回答1:


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.



来源:https://stackoverflow.com/questions/41832843/how-to-preserve-mahapps-metro-styling-when-adding-custom-trigger

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!