locked
Mouseover not working after click RRS feed

  • Question

  • Hi im using a button and im wrote a style for that on MouseOver to change the color
    the style is working when MouseOver is there...but after i click and then MouseOver...
    then MouseOver is not working...i mean color is not changing...
    can anybody help

    here is the code which i used

    <Style x:Key="Header" TargetType="{x:Type Button}">
    <Setter Property="Background" Value="#3D9828"/>
    <Style.Triggers>
    <Trigger Property="IsMouseOver" Value="True">
    <Setter Property="Background" Value="#70af0e"/>
    </Trigger>
    </Style.Triggers>
    </Style>

    Thursday, April 12, 2012 6:26 AM

Answers

  • Hi Isaac,

    This is a very annoying problem. Bascially whats happening is that WPF provides each control with a default template. So your trigger fires (correctly) but then so does the default templates ismouseover trigger.

    to get around this you have to modify the default template (You can get this using blend) or you can specify your own control template.

    Heres a quick example (put this in your window resources)-

      <ControlTemplate x:Key="btnTemplate" TargetType="{x:Type Button}">
                <Border BorderBrush="Black" Background="#3D9828" x:Name="Border" BorderThickness="1" Width="100" Height="40">
                    <ContentPresenter  HorizontalAlignment="Center" VerticalAlignment="Center"/>
                </Border>
                <ControlTemplate.Triggers>
                    <Trigger Property="IsMouseOver" Value="true">
                        <Setter TargetName="Border" Property="Background" Value="#70af0e" />
                    </Trigger>
                </ControlTemplate.Triggers>
            </ControlTemplate>

    Then to use it  -

       <Button Click="Button_Click" Content="hello" Template="{StaticResource btnTemplate}">
               
            </Button>

    i find it frustrating that you have to do soo much to ovverride the default template triggers etc. Hopefully someone on this forum has a better way of doing this

    • Proposed as answer by maran1987 Thursday, April 12, 2012 10:06 AM
    • Unproposed as answer by maran1987 Thursday, April 12, 2012 10:07 AM
    • Proposed as answer by maran1987 Thursday, April 12, 2012 10:07 AM
    • Marked as answer by Issac999 Thursday, April 12, 2012 10:53 AM
    Thursday, April 12, 2012 9:33 AM

All replies

  • Hi Issac, I hope you're fine.

    I believe the following link will provide some help for you issue:

    http://www.codeproject.com/Questions/156394/WPF-background-changes-when-click-and-hover

    I hope this helps.


    Please, mark my reply as Answer if it helps or vote it up if it gives a key to the solution.
    Thanks in advance and good luck.
    Ahmed M. Gamil
    "It is not titles that honor men, but men that honor titles."

    Thursday, April 12, 2012 8:31 AM
  • Hi Gami...the link which u given is not matches to my scenario....failed to to do so

    thanks, waiting for other reply

    Thursday, April 12, 2012 8:59 AM
  • Hi Isaac,

    This is a very annoying problem. Bascially whats happening is that WPF provides each control with a default template. So your trigger fires (correctly) but then so does the default templates ismouseover trigger.

    to get around this you have to modify the default template (You can get this using blend) or you can specify your own control template.

    Heres a quick example (put this in your window resources)-

      <ControlTemplate x:Key="btnTemplate" TargetType="{x:Type Button}">
                <Border BorderBrush="Black" Background="#3D9828" x:Name="Border" BorderThickness="1" Width="100" Height="40">
                    <ContentPresenter  HorizontalAlignment="Center" VerticalAlignment="Center"/>
                </Border>
                <ControlTemplate.Triggers>
                    <Trigger Property="IsMouseOver" Value="true">
                        <Setter TargetName="Border" Property="Background" Value="#70af0e" />
                    </Trigger>
                </ControlTemplate.Triggers>
            </ControlTemplate>

    Then to use it  -

       <Button Click="Button_Click" Content="hello" Template="{StaticResource btnTemplate}">
               
            </Button>

    i find it frustrating that you have to do soo much to ovverride the default template triggers etc. Hopefully someone on this forum has a better way of doing this

    • Proposed as answer by maran1987 Thursday, April 12, 2012 10:06 AM
    • Unproposed as answer by maran1987 Thursday, April 12, 2012 10:07 AM
    • Proposed as answer by maran1987 Thursday, April 12, 2012 10:07 AM
    • Marked as answer by Issac999 Thursday, April 12, 2012 10:53 AM
    Thursday, April 12, 2012 9:33 AM
  • thank u for the reply...

    it worked perfectly for me....

    Thursday, April 12, 2012 10:53 AM