0

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}"/> 
5
  • You have defined the key as Button.Hoverless which is of type of DottedXamlName. This type of specification is used for property and event qualified references, and also for attached members. So remove the . from your key name. Commented Dec 9, 2015 at 11:55
  • Also, the resource created in windows.Resources is a keyed resource so it will apply automatically. So where ever you require this style you should define it as Style="{StaticResource ResourceKeyName}". If you want to apply to all buttons then remove the key attribute. Commented Dec 9, 2015 at 11:57
  • 2
    You're explicitly setting the background on your button to #FFFF9800, which has a higher priority than the setters in your style's triggers. Using Buttonbase as TargetType is perhaps a bit odd but shouldn't be a problem, as Button inherits from ButtonBase. Commented Dec 9, 2015 at 12:11
  • @PieterWitvoet You're right. That did solve it. Would you like to make it an answer? I just had to remove the properties from the button itself. Commented Dec 9, 2015 at 12:17
  • 1
    See my edit for another solution. Commented Dec 9, 2015 at 12:42

1 Answer 1

4

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.

Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.