如何覆盖默认的WPF按钮样式? [英] How to override default wpf button style?

查看:81
本文介绍了如何覆盖默认的WPF按钮样式?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我创建了简单的按钮并应用了样式.

I created simple button and applied style.

<UserControl.Resources>
        <Style x:Key="MyBtnStyle" TargetType="{x:Type Button}">
            <Setter Property="FontSize" Value="12" />
            <Setter Property="FontWeight" Value="Bold" />
            <Setter Property="Foreground" Value="Red" />           
            <Setter Property="BorderThickness" Value="0" />
            <Setter Property="Width" Value="20" />
            <Setter Property="Content" Value="X"/>
            <Style.Triggers>
                <Trigger Property="IsMouseOver" Value="True">
                    <Setter Property="BorderThickness" Value="1"/>
                    <Setter Property="BorderBrush" Value="#FF2B3C59"/>
                    <Setter Property="Background" Value="Green"/>
                </Trigger>
                <Trigger Property="IsPressed" Value="True">
                    <Setter Property="BorderThickness" Value="2"/>
                    <Setter Property="BorderBrush" Value="#FF2B3C59"/>
                </Trigger>
            </Style.Triggers>
        </Style>
    </UserControl.Resources>



和这里按钮:



and here button:

<Button Style="{StaticResource MyBtnStyle}" Name="button1" Click="button1_Click" ></Button>



因此问题出在 IsMouseOver 触发器触发之后,默认的win7按钮样式也会触发.

那么如何防止它触发呢?



so the problem is after IsMouseOver trigger fires, default win7 button style also fires.

So how I can prevent it from firing?

推荐答案



您没有覆盖ControlTemplate-因此仍使用原始模板及其所有触发器.

但是:如果覆盖ControlTemplate,则会摆脱 all 的默认行为,但是必须定义包括所有行为的完整布局.

干杯
于尔根(Jürgen)
Hi,

you didn''t overwrite the ControlTemplate - so the original template is still used and with it all it''s triggers.

But: if you overwrite the ControlTemplate, you get rid of all default behaviours, but you have to define the complete layout including all behaviours on your own.

Cheers
Jürgen


我唯一能想到的就是因为您没有重写整个样式/模板. Microsoft可以下载所有标准模板,或者如果您有Expression Blend,则可以只创建模板的副本.
The only thing I can think of is it''s because you haven''t overridden the ENTIRE style/template. Microsoft has all of the standard templates available as a download, or if you have Expression Blend, you can just create a copy of the template.


这篇关于如何覆盖默认的WPF按钮样式?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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