views:

140

answers:

1

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
alt text

and here is what it looks like when it is clicked
alt text

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)

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

+1  A: 

The problem lies in the default control template for windows xp theme luna. If you change the theme in display properties to classic your style will work.

It should be possible to override the system colors in wpf, but i haven't been able to solve your problem with that solution, maybe the color is frozen so it cannot be changed or the class for creating a theme-specific look, ButtonChrome, is broken.

I think that your best bet is to make your own control template for the button.

Oggy
I will give this a go, im reluctant to make a controltemplate for the button since this style is a generic across ALL of my buttons. I'll have a go tho
TerrorAustralis
Thanks mate! it worked nicely. I control templated the button and now my background changes on click
TerrorAustralis
If you're afraid of applying your own templates for fear of missing any necessary parts. Then try using Microsoft Expression Blend to extract both the styles and control templates for different controls.
Domokun