A
A
aydep2021-11-15 17:50:09
WPF
aydep, 2021-11-15 17:50:09

How to change button background in WPF on mouse hover?

Hey! There is a problem with the fact that I am trying to add styles to a button. All work fine, except for the background change on hover. I register a trigger with a background color change, but it does not change the color to the one I specified. I understand that there is some standard style that sets the background on hover, which I cannot change.

<Application.Resources>
        <Style x:Key="cdButton" TargetType="Button">
            <Setter Property="Background" Value="#3D5754" />
            <Style.Triggers>
                <Trigger Property="IsMouseOver" Value="True">
                    <Setter Property="Background" Value="Red" />
                </Trigger>
            </Style.Triggers>
        </Style>
    </Application.Resources>


<Button Content="Войти" Margin="0 20" Click="button_Sign_Click" Style="{StaticResource cdButton}" >


Shows the following without hover: 61927351712d7243115814.png
Shows with hover: 6192735912146505049897.png
What you would like on hover:61927362d0966766659128.png

Answer the question

In order to leave comments, you need to log in

3 answer(s)
E
Ertanic, 2021-11-16
@aydep

You need to change the button template and bind the button background color with the border background color:

<Style TargetType="{x:Type Button}">
    <Setter Property="Background" Value="#3D5754"/>
    <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="Red"/>
        </Trigger>
    </Style.Triggers>
</Style>

E
Elena, 2015-03-05
@Nidora

What is MemberLux or paid access on WordPress
WP Pay Access

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question