Resource

How to add mouse rollover affects to a WPF Button?

Soft Dev Resources Jun 20, 2009
How to add mouse rollover affects to a WPF Button?

The following code snippet adds Mouse Enter and Mouse Leave event handlers.

<Button x:Name="DrawCircleButton" Height="40" Width="120"

        Canvas.Left="10" Canvas.Top="10" Click="DrawCircleButton_Click"

        MouseEnter="DrawCircleButton_MouseEnter" MouseLeave="DrawCircleButton_MouseLeave">

The code listed in Listing 4 sets the background and foreground colors of a Button on mouse enter and mouse leave event handlers.

private void DrawCircleButton_MouseEnter(object sender, MouseEventArgs e)

{

    DrawCircleButton.Background = new SolidColorBrush(Colors.Yellow );

    DrawCircleButton.Foreground = new SolidColorBrush(Colors.Green);

}

 

private void DrawCircleButton_MouseLeave(object sender, MouseEventArgs e)

{

    DrawCircleButton.Background = new SolidColorBrush(Colors.Red);

    DrawCircleButton.Foreground = new SolidColorBrush(Colors.Purple);

}

Listing 4

COMMENT USING