Mouse Event of Silverlight 3(with Full Source Code)
Silverlight 3 provides us some mouse events, containing MouseEnter, MouseLeave,
MouseLeftButtonDown, MouseLeftButtonUp, MouseMove, and MouseWheel. These mouse events use MouseButtonEventArgs and MouseEventArgs as parameters to catch events then active related behaviour. Through the passage we’ll learn to manage five of these events above. Please prepare some nice pictures and then let’s start to practise three samples with VWD or Visual Studio 2008.
MouseEnter & MouseLeave
Here we’ll realize such an effect: when the cursor move onto the original image, it turns into another image; when the cursor leave the image field, the second image changes into a third pic. Therefore the MouseEnter & MouseLeave events are needed.
Let’s first put an image into the page, with two mouse events "Image_MouseEnter" and "Image_MouseLeave".
- <canvas>
- <image Source="roseheart.jpg" Canvas.Left="60" Canvas.Top="70"
- MouseEnter="Image_MouseEnter"
- MouseLeave="Image_MouseLeave">
- </image>
- </canvas>
Then complete the two functions in MainPage.xaml.cs.
- private void Image_MouseEnter(object sender, MouseEventArgs e)
- {
- Image img = sender as Image;
- img.Source = new BitmapImage(new Uri("fourleaf.jpg",UriKind.RelativeOrAbsolute));
- }
- private void Image_MouseLeave(object sender, MouseEventArgs e)
- {
- Image img = sender as Image;
- img.Source = new BitmapImage(new Uri("rose.jpg", UriKind.RelativeOrAbsolute));
- }
Read the code above, the function Image_MouseEnter defines that when the mouse move onto roseheart.jpg, the pic will change into fourleaf.jpg. For that the picture format supported by Silverlight 3 is limited, we use BitmapImage to load a new pic of JPEG format, that’s alright too. To load files, like images, we use Uri Class, its constructor is (String, UriKind). For more information you can refer to MSDN. Remember to add a line "using System.Windows.Media.Imaging;" to the head of MainPage.xaml.cs.
So does the function Image_MouseLeave mean and you can get it.
MouseLeftButtonUp & MouseLeftButtonDown
In this sample, we create an image field and a text block. When mouse is left-clicked onto the image, the text change. When the left mouse button is released, the text changes again.
Insert the code below to MainPage.xaml:
- <image Source="leaf.jpg" Canvas.Left="450" Canvas.Top="150"
- MouseLeftButtonDown="Image_MouseLeftButtonDown"
- MouseLeftButtonUp="Image_MouseLeftButtonUp">
- </image>
- <border BorderBrush="Gold" BorderThickness="4" Canvas.Left="120" Canvas.Top="20">
- <textblock x:Name="textBlock" Text="Please click the green leaf!"
- Width="500" Height="40"
- FontSize="25" Foreground="Blue"/>
- </border>
Next add code to MainPage.xaml.cs:
- private void Image_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
- {
- textBlock.Foreground = new SolidColorBrush(Colors.Orange);
- textBlock.Text = "Welcome to Silverlight 3 World!";
- }
- private void Image_MouseLeftButtonUp(object sender, MouseButtonEventArgs e)
- {
- textBlock.Foreground = new SolidColorBrush(Colors.Purple);
- textBlock.FontSize = 25;
- textBlock.Text = "Wish you a wonderful Silverlight trip!";
- }
MouseMove
We use MouseMove event to show the cursor’s current position. To do this, just add one row into Canvas Control markup:
- <canvas MouseMove="Canvas_MouseMove">
- </canvas>
To show the position in text, add the code:
- <textblock x:Name="Position" Text="Position" Foreground="Salmon"
- FontSize="20" Canvas.Left="500" Canvas.Top="350">
- </textblock>
The related function is like this:
- private void Canvas_MouseMove(object sender, MouseEventArgs e)
- {
- Point p = e.GetPosition(this);
- Position.FontSize = 15;
- Position.Text = string.Format("Position({0}:{1})", p.X, p.Y);
- }
Run the project, the page shows like the following pic:
Move mouse so that the cursor is on the left picture, the pic changes and the cursor position shows:
When the mouse leaves left pic, click the right one and keep left button not released, you can see both the left pic and the text block’s content change.
Release left mouse button, the text changes again:
By now you are able to manage the mouse event in Silverlight 3. The source code of the three sample has been attached and you can download it as a reference
here.
For help, advice, tips and tricks, challenges, feel free to visit our
or Submit your good resource to share.
Reminder: Unless stated otherwise, all resources published on this site are NOT for commercial use. To use any resource from this site for commercial purposes, please contact the author.

