1

I don't understeand why this code doesn't work.

The button color is still default when I hover my mouse over it.

<Window>
    <Window.Resources>

        <Style x:Key="hover" TargetType="Button">
            <Style.Triggers>
                <Trigger Property="IsMouseOver" Value="True">
                    <Setter Property="Background" Value="Red" />
                </Trigger>
            </Style.Triggers>
        </Style>

    </Window.Resources>

    <Grid>
        <Button
            Width="144"
            Height="58"
            Margin="320,177,0,0"
            HorizontalAlignment="Left"
            VerticalAlignment="Top"
            Content="Button" 
            Style="{StaticResource hover}" />
    </Grid>
</Window>

screen

ASh
  • 34,632
  • 9
  • 60
  • 82
pogu
  • 11
  • 2
  • 2
    Hello have You seen this [question](https://stackoverflow.com/questions/17259280/how-do-you-change-background-for-a-button-mouseover-in-wpf) ? – Kuba Do Oct 18 '19 at 11:24
  • 2
    Possible duplicate of [How do you change Background for a Button MouseOver in WPF?](https://stackoverflow.com/questions/17259280/how-do-you-change-background-for-a-button-mouseover-in-wpf) – Corentin Pane Oct 18 '19 at 11:29

1 Answers1

0

it is necessary to modify the ControlTemplate to activate the IsMouseOver event like this :

<Button Width="144" Height="58" Margin="320,177,0,0" HorizontalAlignment="Left" VerticalAlignment="Top" Content="Button" >
                <Button.Style>
                    <Style TargetType="Button">
                        <Setter Property="Template">
                            <Setter.Value>
                                <ControlTemplate TargetType="Button">
                                    <Border Background="{TemplateBinding Background}" BorderBrush="Black" BorderThickness="1">
                                        <ContentPresenter HorizontalAlignment="Center" VerticalAlignment="Center"/>
                                    </Border>
                                </ControlTemplate>
                            </Setter.Value>
                        </Setter>
                        <Style.Triggers>
                            <Trigger Property="IsMouseOver" Value="True">
                                <Setter Property="Background" Value="Red"/>
                            </Trigger>
                        </Style.Triggers>
                    </Style>
                </Button.Style>
            </Button>
Quentin Couderc
  • 86
  • 2
  • 12