Button Style Trigger - IsPressed Property

Hi guys,

May be someone knows why I can't change button's background using style trigger in IsPressed Property.

Appreciate your help.

Irina

<Window x:Class="Window1"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
>

<Window.Resources>

<Style TargetType="{x:Type Button}">
<Style.Triggers>
<Trigger Property="IsPressed" Value="true">
<Setter Property="Background" Value="Red"/>
</Trigger>
</Style.Triggers>
</Style>
</Window.Resources>

<StackPanel VerticalAlignment="Center" Orientation="Horizontal">
<Button Background="AliceBlue">Click me!</Button>
</StackPanel>

</Window>




Answer this question

Button Style Trigger - IsPressed Property

  • Balasubramaniam K

    Thanks for answering but it does not change color anyway even if

    <Setter Property="Background" Value="Red"/> is the only place you change the background

    <Window x:Class="Window1"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
    <Window.Resources>
    <Style TargetType="{x:Type Button}">
    <Setter Property="Background" Value="AliceBlue"></Setter>
    <Style.Triggers>
    <Trigger Property="IsPressed" Value="true">
    <Setter Property="Background" Value="Red"/>
    </Trigger>
    </Style.Triggers>
    </Style>
    </Window.Resources>
    <StackPanel VerticalAlignment="Center" Orientation="Horizontal">
    <Button>Click me!</Button>
    </StackPanel>
    </Window>



  • ABinBoston

    It does not work either. I think the only problem is in Property "IsPressed". Using any other properties like"IsMouseOver" - background changes.

  • Mibe

    Try this instead:

    <Window x:Class="Window1"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
    <StackPanel VerticalAlignment="Center" Orientation="Horizontal">

    <Button Background="Black">
    <Button.Style>
    <Style TargetType="{x:Type Button}">
    <Style.Triggers>
    <Trigger Property="IsPressed" Value="true">
    <Setter Property="Background" Value="Red" />
    </Trigger>
    </Style.Trigger>
    </Style>
    </Button.Style>
    Click me!
    </Button>
    </StackPanel>
    </Window>

    HTH

    Bye



  • taotor

    It is because of the dependency property precedence.
    http://windowssdk.msdn.microsoft.com/library/en-us/wpf_conceptual/html/1fbada8e-4867-4ed1-8d97-62c07dad7ebc.asp
    There is a good explanation regarding your sample in the "Dependency Properties Might be "Set" in Multiple Places" section.

    So, in order to make the sample work, you should set the background to AliceBlue in a Style Setter. Dependency Properties Might be "Set" in Multiple Places

  • Sun - By - Night

    Just to make sure, I have used the code from the second post. I have tested on both Win XP SP2 and 2003 Server with Feb 2006 CTP and it works fine.


  • aak165842

    Strange! I have used the code from your second post - it works on my computer, when I click on the button the background changes to red.


  • fwpi

    I've used the same code but it doesn't work....

    My button are colorized in AliceBlue (normal) but they never change to Red.

    I'm using Windows XP SP2 and the Feb 2006 CTP



  • Chernichkin Stanislav

    I'm experiencing the same problem as Irina above. Are you (viliescu) using the February 2006 CTP as implied by the XML namespaces On XP Is this working for anybody else
  • Button Style Trigger - IsPressed Property