OmniOwl
OmniOwl

Reputation: 5709

Styles are not applied

I have the following triggers in a custom button style:

<Trigger Property="IsEnabled" Value="true">
    <Setter Property="BorderBrush" Value="#FFFFFFFF" />
    <Setter Property="Foreground" Value="#FFFFFFFF" />
</Trigger>
<Trigger Property="IsEnabled" Value="false">
    <Setter Property="BorderBrush" Value="#FFFF9800" />
    <Setter Property="Foreground" Value="#FFFF9800" />
</Trigger>

But they don't apply in my GUI at all. Below is the entire Style.

<Window.Resources>
    <Style x:Key="Button.Hoverless" TargetType="{x:Type ButtonBase}">
        <Setter Property="Template">
            <Setter.Value>
                <ControlTemplate TargetType="{x:Type ButtonBase}">
                    <Border Name="border"
                        BorderThickness="{TemplateBinding BorderThickness}"
                        Padding="{TemplateBinding Padding}"
                        BorderBrush="{TemplateBinding BorderBrush}"
                        Background="{TemplateBinding Background}">
                        <ContentPresenter HorizontalAlignment="Center" VerticalAlignment="Center" />
                    </Border>
                    <ControlTemplate.Triggers>
                        <MultiTrigger>
                            <MultiTrigger.Conditions>
                                <Condition Property="IsMouseOver" Value="True" />
                                <Condition Property="Selector.IsSelected" Value="False" />
                            </MultiTrigger.Conditions>
                            <Setter Property="Background" Value="#FFFF9800" />
                            <Setter Property="Opacity" Value="0.50" />
                        </MultiTrigger>

                        <MultiTrigger>
                            <MultiTrigger.Conditions>
                                <Condition Property="IsMouseOver" Value="True" />
                                <Condition Property="Selector.IsSelected" Value="True" />
                            </MultiTrigger.Conditions>
                            <Setter Property="Background" Value="#FFFFB850" />
                        </MultiTrigger>

                        <MultiTrigger>
                            <MultiTrigger.Conditions>
                                <Condition Property="IsMouseOver" Value="False" />
                                <Condition Property="Selector.IsSelected" Value="True" />
                            </MultiTrigger.Conditions>
                            <Setter Property="Background" Value="#FFFF9800" />
                        </MultiTrigger>

                        <Trigger Property="IsEnabled" Value="true">
                            <Setter Property="BorderBrush" Value="#FFFFFFFF" />
                            <Setter Property="Foreground" Value="#FFFFFFFF" />
                        </Trigger>
                        <Trigger Property="IsEnabled" Value="false">
                            <Setter Property="BorderBrush" Value="#FFFF9800" />
                            <Setter Property="Foreground" Value="#FFFF9800" />
                        </Trigger>

                        <Trigger Property="IsPressed" Value="True">
                            <Setter TargetName="border" Property="Opacity" Value="0.95" />
                        </Trigger>
                    </ControlTemplate.Triggers>
                </ControlTemplate>
            </Setter.Value>
        </Setter>
    </Style>
</Window.Resources>

I'm new to WPF styling and thus I'm not sure where I went wrong with this. Also included is the button code:

<Button x:Name="btnScanFP1" Content="Scan Fingerprint" Grid.Column="1" 
        HorizontalAlignment="Left" Margin="10,10,0,0" Grid.Row="4" VerticalAlignment="Top"
        Width="227" Height="21" Grid.ColumnSpan="2" FontSize="11" Click="btnScanFP1_Click"
        Background="{x:Null}" BorderBrush="#FFFF9800" Foreground="#FFFF9800"
        Style="{StaticResource Button.Hoverless}"/>

Upvotes: 0

Views: 140

Answers (1)

Pieter Witvoet
Pieter Witvoet

Reputation: 2833

According to the Dependency Property Setting Precedence List, explicitly setting a property has a higher priority than a style trigger (which itself has a higher priority than a style setter).

Removing BorderBrush="#FFFF9800" from your button will allow the style triggers to alter the border brush as expected. The same goes for the Foreground property.

But perhaps a better solution is to refer to the border in your control template by name:

<Trigger Property="IsEnabled" Value="True">
    <Setter TargetName="border" Property="BorderBrush" Value="#FFFFFFFF" />
</Trigger>

This overrides the template-binding on the border's BorderBrush property, so it will apply even when you explicitly set the BorderBrush property on your button.

Upvotes: 4

Related Questions