Learn Silverlight 2 series (4): Mouse Event handling

Source: Internet
Author: User

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:

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.