C# – How to change the button color onmoveover,onmouseleave in wpf by using triggers or any other events


I have developed a WPF Application with some buttons. Now i want to change the color of those buttons onmouseover,onmouseleave,onmouseenter by using triggers or any other events.
Any suggestion plz
Thanks in advance.

Best Answer

Inside the desired event, you can set the background color like this...

// Change the background color of button1 to Blue
button1.Background = Brushes.Blue;

You can also set this in a trigger:

<!-- Button will change from Blue to Yellow on MouseOver -->
<Style TargetType="{x:Type Button}">
    <Setter Property="Background" Value="Blue" />
        <Trigger Property="IsMouseOver" Value="True">
            <Setter Property="Background" Value="Yellow" />

For even more details, check out the Property Triggers section of this article.