In the fourth installment of this article, learn about mouse event handling in Silverlight 2, supported mouse events including MouseMove, MouseEnter, MouseLeave, MouseLeftButtonDown, MouseLeftButtonUp.
declaring events
For mouse events, we can attach to any Silverlight object, such as the following XAML declaration, to add the MouseEnter and MouseLeave events for two circles:
<Canvas Background="#46461F">
<Ellipse Width="120" Height="120" Fill="Orange"
Canvas.Top="60" Canvas.Left="80"
MouseEnter="OnMouseEnter"
MouseLeave="OnMouseLeave"/>
<Ellipse Width="120" Height="120" Fill="Orange"
Canvas.Top="60" Canvas.Left="280"
MouseEnter="OnMouseEnter"
MouseLeave="OnMouseLeave"/>
</Canvas>
Write an event handler that changes the fill color of the circle when the mouse is on and when the mouse moves away:
void OnMouseEnter(object sender, MouseEventArgs e)
{
Ellipse ell = sender as Ellipse;
ell.Fill = new SolidColorBrush(Colors.Yellow);
}
void OnMouseLeave(object sender, MouseEventArgs e)
{
Ellipse ell = sender as Ellipse;
ell.Fill = new SolidColorBrush(Colors.Green);
}
After the run effect is as follows: