Why is button background defaulting to grey when IsPressed is true

WPF按钮样式定制
本文介绍了一种在WPF中自定义按钮样式的方法,通过创建控制模板并进行数据绑定,解决了按钮按下时背景颜色无法改变的问题。

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

 

问题出现原因:

 

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.

 

 

转载于:https://www.cnblogs.com/mrchenzh/archive/2012/05/26/2519157.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值