Hey all,
I have a simple problem. Using the IsPressed trigger i want to be able to set the background color of a button to something other than the default grey. Here is what the button looks like when it is not pressed
and here is what it looks like when it is clicked
Here is the trigger for the button. I know the trigger is firing correctly because of the glow effect around the edge of the button when it is clicked. I also know that the brush is correct because i tried it out as a background brush to see what it looked like.
<Style.Triggers>
<Trigger Property="IsMouseOver" Value="True">
<Setter Property="Background" Value="{DynamicResource ButtonHoverBrush}"/>
<Setter Property="BitmapEffect" Value="{DynamicResource ButtonHoverGlow}"/>
</Trigger>
<!-- This is the trigger which is working but the background color wont change -->
<Trigger Property="IsPressed" Value="True">
<Setter Property="BitmapEffect" Value="{DynamicResource ButtonHoverGlow}"/>
<Setter Property="Background" Value="{DynamicResource ButtonPressedBrush}" />
</Trigger>
</Style.Triggers>
Here is the entire style, as you can see, it is a default style applying to all buttons across the application.
<Style TargetType="Button">
<Setter Property="Background" Value="{DynamicResource ButtonBrush}" />
<Setter Property="Foreground" Value="Black" />
<Style.Triggers>
<Trigger Property="IsMouseOver" Value="True">
<Setter Property="Background" Value="{DynamicResource ButtonHoverBrush}"/>
<Setter Property="BitmapEffect" Value="{DynamicResource ButtonHoverGlow}"/>
</Trigger>
<Trigger Property="IsPressed" Value="True">
<Setter Property="BitmapEffect" Value="{DynamicResource ButtonHoverGlow}"/>
<Setter Property="Background" Value="{DynamicResource ButtonPressedBrush}" />
</Trigger>
</Style.Triggers>
</Style>
Here is the final solution(Thank you to all who helped)
Create a control template for a generic button and do some data binding:
<ControlTemplate TargetType ="Button" x:Key="ButtonControlTemplate"> <Border Background="{TemplateBinding Background}" BorderThickness="{TemplateBinding BorderThickness}" BorderBrush="{TemplateBinding BorderBrush}" CornerRadius="3"> <Grid> <ContentPresenter ContentSource="{TemplateBinding Content}" ContentTemplate="{TemplateBinding ContentTemplate}" VerticalAlignment="{TemplateBinding VerticalContentAlignment}" HorizontalAlignment="{TemplateBinding HorizontalContentAlignment}"/> </Grid> </Border> </ControlTemplate>
Add control template to style:
<Style TargetType="Button"> <Setter Property="Background" Value="{DynamicResource ButtonBrush}" /> <Setter Property="Foreground" Value="Black" /> <Setter Property="Template" Value="{DynamicResource ButtonControlTemplate}" /> <Setter Property="BorderThickness" Value="1" /> <Style.Triggers> <Trigger Property="IsMouseOver" Value="True"> <Setter Property="Background" Value="{DynamicResource ButtonHoverBrush}"/> <Setter Property="BitmapEffect" Value="{DynamicResource ButtonHoverGlow}"/> </Trigger> <Trigger Property="IsPressed" Value="True"> <Setter Property="BitmapEffect" Value="{DynamicResource ButtonHoverGlow}"/> <Setter Property="Background" Value="{DynamicResource ButtonPressedBrush}" /> </Trigger> </Style.Triggers> </Style>
Note: BorderThickness is defaulted to 1 because otherwise it doesnt show