简体   繁体   English

WPF从按钮样式覆盖触发器

[英]WPF Override a trigger from a button style

I have below button style in window resources: 我在窗口资源中有以下按钮样式:

<Style x:Key="MyStyle" TargetType="{x:Type Button}">

    <Setter Property="BorderBrush" Value="Transparent"/>
    <Setter Property="BorderThickness" Value="0 0 0 3"/>

    <Style.Triggers>
        <Trigger Property="IsMouseOver" Value="True">
            <Setter Property="BorderBrush" Value="Orange" />
        </Trigger>
        <Trigger Property="IsPressed" Value="True">
            <Setter Property="BorderBrush" Value="Red" />
        </Trigger>         
    </Style.Triggers>

</Style>

This style is shared by two wpf buttons. 这种风格由两个wpf按钮共享。 But there is a button I want to show a custom color when it is pressed, the color will be green. 但是有一个按钮,我想在按下时显示自定义颜色,颜色将为绿色。

So in this special button I would like to override the value specified for borderbrush property in the trigger, instead of Red I would like Green. 所以在这个特殊的按钮中我想覆盖触发器中为borderbrush属性指定的值,而不是Red我想要Green。

How to do this? 这该怎么做?

You could set the BorderBrush property using a {DynamicResource} that you can override: 您可以使用可以覆盖的{DynamicResource}设置BorderBrush属性:

<SolidColorBrush x:Key="pressed" Color="Red" />
<Style x:Key="MyStyle" TargetType="{x:Type Button}">
    <Setter Property="BorderBrush" Value="Transparent"/>
    <Setter Property="BorderThickness" Value="0 0 0 3"/>
    <Style.Triggers>
        <Trigger Property="IsMouseOver" Value="True">
            <Setter Property="BorderBrush" Value="Orange" />
        </Trigger>
        <Trigger Property="IsPressed" Value="True">
            <Setter Property="BorderBrush" Value="{DynamicResource pressed}" />
        </Trigger>
    </Style.Triggers>
</Style>
...
<Button Content="Red" Style="{StaticResource MyStyle}" />

<Button Content="Green" Style="{StaticResource MyStyle}">
    <Button.Resources>
        <SolidColorBrush x:Key="pressed" Color="Green" />
    </Button.Resources>
</Button>

Or you could create another Style that overrides the entire trigger: 或者您可以创建另一个覆盖整个触发器的Style

<Button Content="Green">
    <Button.Style>
        <Style TargetType="Button" BasedOn="{StaticResource MyStyle}">
            <Style.Triggers>
                <Trigger Property="IsPressed" Value="True">
                    <Setter Property="Foreground" Value="Green" />
                </Trigger>
            </Style.Triggers>
        </Style>
    </Button.Style>
</Button>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM