添加自定义触发器时如何保留 mahapps Metro 样式? [英] How to preserve mahapps metro styling when adding custom trigger?

查看:20
本文介绍了添加自定义触发器时如何保留 mahapps Metro 样式?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个按默认地铁样式设计的按钮.但是,当我向按钮添加触发器时,样式被覆盖.添加自己的样式和触发器时如何保留原始的 mahapps Metro 样式?

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 it looks after I added the trigger

推荐答案

您需要将样式的BasedOn"属性设置为 mahapps.metro 提供的默认属性:

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

文档没有说明默认的静态资源,但是在您导入的 Controls.xaml 的源代码中跟踪它是很容易的,以便在您的 app.xaml(或窗口顶部等)中加载默认样式):

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

搜索 TargetType="Button" 会找到我们的默认样式(没有键):

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

甚至还基于基本样式,MahApps.Metro.Styles.MetroButton.

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

这篇关于添加自定义触发器时如何保留 mahapps Metro 样式?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆