Geeks With Blogs
DotNet Snippets Silverlight and C#

Hello Guys,

Early this morning I’m thinking about the draggable Image in Silverlight. I created a sample with simple snippets.

1. Create one Image, rectangle, and button.

2. Create an event handler for Image.

this.image1.MouseMove += new MouseEventHandler(image1_MouseMove);
this.image1.MouseLeftButtonDown += new MouseButtonEventHandler(image1_MouseLeftButtonDown);
this.image1.MouseLeftButtonUp += new MouseButtonEventHandler(image1_MouseLeftButtonUp);

3. Setting a variable
private bool isDragging = false; //dragging?
private Point mousePosition; //position of mouse

4. And now the events

void image1_MouseLeftButtonUp(object sender, MouseButtonEventArgs e)
isDragging = false;
} //Release the mouse

void image1_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
mousePosition = e.GetPosition(sender as UIElement);
isDragging = true;
} //Pressed the left mouse button

void image1_MouseMove(object sender, MouseEventArgs e)
if (isDragging)
this.imgTranslateTransform.X = e.GetPosition(this).X - mousePosition.X;
this.imgTranslateTransform.Y = e.GetPosition(this).Y - mousePosition.Y;
} //Dragging the Image

private void btnBrowse_Click(object sender, RoutedEventArgs e)
OpenFileDialog ofd = new OpenFileDialog();

BitmapImage imgSrc = new BitmapImage();
image1.Source = imgSrc;
textBlock1.Text = "You can now drag the Image";
} // Open/Browsing the Image (binding Image)

Posted on Thursday, February 25, 2010 1:14 PM C# , ASP.NET , Silverlight | Back to top

Comments on this post: Draggable Image on Silverlight

# re: Draggable Image on Silverlight
Requesting Gravatar...
you are doing the above coding in silverlight part of visual studio 2010 or in the expresssion blend 4?
please reply..
Left by vipul malhotra on Dec 02, 2011 5:49 AM

Your comment:
 (will show your gravatar)

Copyright © Sayre Collado | Powered by: