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

Jamie Law
  • 11
  • 1
  • 1
  • 6
  • 1
    you cann override Button Template. See it [here](https://stackoverflow.com/questions/17630968/wpf-c-sharp-button-style) – Stefan W. Jan 30 '18 at 13:52
  • If It's a listview you can just disable selection on the listview or use ItemsControl – csharpwinphonexaml Jan 30 '18 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. – Mike Strobel Jan 30 '18 at 14:14
  • @Stefan i don't see which part of your linked post is non code related? – Jamie Law Jan 30 '18 at 14:39
  • it was "here" ;) https://stackoverflow.com/questions/17630968/wpf-c-sharp-button-style – Stefan W. Jan 30 '18 at 14:39
  • i don't understand? – Jamie Law Jan 30 '18 at 15:26
  • It can be helpfully: https://stackoverflow.com/questions/43740207/how-can-i-change-the-default-button-highlight-in-wpf-c-sharp – Rekshino Jan 30 '18 at 15:59

1 Answers1

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>
Swapnil
  • 66
  • 7