0

In my C# WPF application, I have 3 buttons, each is assigned image brush backgrounds just after the InitializeComponent() function in the MainWindow Constructor. when I hover my mouse over each of the buttons, they turn to a solid blue colour. I figured I needed to add MouseEnter methods to each button, along with MouseLeave ones. changing the button to a darker version when hovered.

But the image still turns blue when hovered... See MP4 for explanation

https://i.gyazo.com/06d936daaf5153c369b2b9af2faa1a9f.mp4

Thank You

J

7
  • 1
    you cann override Button Template. See it here Commented Jan 30, 2018 at 13:52
  • If It's a listview you can just disable selection on the listview or use ItemsControl Commented Jan 30, 2018 at 14:07
  • No code changes are required. You simply need to define a custom template, as Stefan says. The default template changes the background based on the state of the button, and it only respects the background when it's in its 'normal' state. Follow the link he provided for an example. Commented Jan 30, 2018 at 14:14
  • @Stefan i don't see which part of your linked post is non code related? Commented Jan 30, 2018 at 14:39
  • it was "here" ;) stackoverflow.com/questions/17630968/wpf-c-sharp-button-style Commented Jan 30, 2018 at 14:39

1 Answer 1

0

You can use following Style on Buttons to achieve the required outcome , In case you need to use another color on mouse over just change the border foreground property within trigger below , in case if dont want any of mouse over effect then just change the foreground to Transperent

<Style TargetType="Button">
     <Setter Property="OverridesDefaultStyle" Value="True"/>               
     <Setter Property="Template">
           <Setter.Value>
                   <ControlTemplate TargetType="Button">
                       <Border Name="border" 
                        BorderThickness="1"
                        Padding="4,2" 
                        BorderBrush="DarkGray" 
                        CornerRadius="3" 
                        Background="{TemplateBinding Background}">
                    <ContentPresenter HorizontalAlignment="Center" VerticalAlignment="Center" />
                      </Border>
                <ControlTemplate.Triggers>
                       <Trigger Property="IsMouseOver" Value="True">
              <Setter TargetName="border" Property="Background" Value="Blue" />
                         </Trigger>
             </ControlTemplate.Triggers>
            </ControlTemplate>
         </Setter.Value>
       </Setter>
   </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.