0

I'm trying to change my button color when the mouse is over it but it's not working (the button is still blue) and all examples I find go like I'm doing:

<Button.Style>
    <Style TargetType="{x:Type Button}">
        <Setter Property="Background" Value="#424242"/>
        <Style.Triggers>
            <Trigger Property="IsMouseOver" Value="True">
                <Setter Property="Background" Value="#8BC34A"/>
            </Trigger>
        </Style.Triggers>
    </Style>
</Button.Style>

What am I doing wrong?

4

1 Answer 1

3

You should use ControlTemplate for this purpose like this:

<Button.Style>
     <Style TargetType="{x:Type Button}">
          <Setter Property="Background" Value="#424242"/>
          <Setter Property="Template">
              <Setter.Value>
                  <ControlTemplate TargetType="{x:Type Button}">
                       <Border Background="{TemplateBinding Background}">
                          <ContentPresenter HorizontalAlignment="Center" VerticalAlignment="Center"/>
                      </Border>
                  </ControlTemplate>
              </Setter.Value>
         </Setter>
         <Style.Triggers>
             <Trigger Property="IsMouseOver" Value="True">
                 <Setter Property="Background" Value="#8BC34A"/>
             </Trigger>
           </Style.Triggers>
     </Style>
</Button.Style>
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.