WPF Override a trigger from a button style
Asked Answered
L

1

6

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. 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.

How to do this?

Longlived answered 4/7, 2017 at 11:49 Comment(0)
V
13

You could set the BorderBrush property using a {DynamicResource} that you can override:

<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:

<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>
Viperous answered 4/7, 2017 at 12:9 Comment(4)
I like your second solution. My button has a controlTemplate defined so in which place exactly within controltemplate I have to put the style? Sorry I am very new in WPF...Longlived
I don't think I understand your question. I gave you an example of how to override the trigger for the customized Button, i.e. you define a specific Style for this one just like I showed you.Viperous
Your answer is correct. I'll try to explain: my button has a template defined like this: <Button> <Button.Template><ControlTemplate>....</ControlTemplate></Button.Template></Button> So now I do not know how to put your piece of code within the button.Longlived
Please ask a new question if you have another issue. Don't ask additional questions in the comments field.Viperous

© 2022 - 2024 — McMap. All rights reserved.