Drag and drop Silverlight example
This is a quick and simple example of how to create a draggable object in Silverlight 2.
- Create a Rectangle and set the RenderTransform to a TranslateTransform.
- Register the MouseLeftButtonDown, MouseLeftButtonUp, and the MoseMove events for the rectangle.
- In the MouseLeftButtonDown and MouseLeftButtonUp event you will capture and release the capture of the mouse, respectively. Also, you will need to store if the mouse is captured.
- Finally, update the position if the mouse is captured and the mouse is moving.
<Rectangle x:Name="rect" Width="50" Height="50" Fill="Blue" HorizontalAlignment="Left" VerticalAlignment="Top">
<Rectangle.RenderTransform>
<TranslateTransform x:Name="rectTranslateTransform" X="0" Y="0" />
</Rectangle.RenderTransform>
</Rectangle>
this.rect.MouseLeftButtonDown += new MouseButtonEventHandler(rect_MouseLeftButtonDown);
this.rect.MouseLeftButtonUp += new MouseButtonEventHandler(rect_MouseLeftButtonUp);
this.rect.MouseMove += new MouseEventHandler(rect_MouseMove);
// Stores if the mouse is captured private Boolean isRectMouseCapture = false; void rect_MouseLeftButtonUp(object sender, MouseButtonEventArgs e) { this.rect.ReleaseMouseCapture(); isRectMouseCapture = false; } void rect_MouseLeftButtonDown(object sender, MouseButtonEventArgs e) { this.rect.CaptureMouse(); isRectMouseCapture = true; }
void rect_MouseMove(object sender, MouseEventArgs e) { if (isRectMouseCapture) { this.rectTranslateTransform.X = e.GetPosition(this).X; this.rectTranslateTransform.Y = e.GetPosition(this).Y; } }
Final product:
Maintain mouse position on object while dragging
The above example snaps the pointer to the top left of the object. Face it, it’s annoying, what you really want is the mouse to maintain the position on the element while dragging. To solve this problem is simple.
- Add a variable to save the position when the user clicks the object.
- Save the position of where the mouse is located on the MouseLeftButtonDown event. The updated MouseLeftButtonDown event looks like:
- Finally, in MouseMove event subtract the clickPosition from the mouse position.
private Point clickPosition;
void rect_MouseLeftButtonDown(object sender, MouseButtonEventArgs e) { clickPosition = e.GetPosition(sender as UIElement); this.rect.CaptureMouse(); isRectMouseCapture = true; }
this.rectTranslateTransform.X = e.GetPosition(this).X - clickPosition.X; this.rectTranslateTransform.Y = e.GetPosition(this).Y - clickPosition.Y;
Update
Drag and drop with physics (friction)
After playing around, I have updated this example to incorporate physics. Take a look:
http://www.coreyschuman.com/Silverlight/Prototypes/DragDropPhysics/Proto1TestPage.aspx
9 comments so far
Leave a reply
[...] Drag and Drop Silverlight Example (Corey Schuman) [...]
It really is made simple! Thanks a lot for this tutorial.
Nice work mate, makes it simple, gonna make sure it now snaps itself into a grid and then we got something that is really useful in applications.
Thanks for the suggestion. I have just completed adding friction to this example. Snapping to a grid will be my next example. I’ll post when I am finished.
you are wrong :
this.rect.MouseMove += new MouseEventHandler(rect_MouseMove);
you must attach the page mousemove event… if you don’t do that, when you move your cursor quickly your drag stop.
pierre, I haven’t experienced this problem. Registering MouseMove for only the rectangle takes care of business.
Thanks for the example!
I found that with this code you need to place your dragable object in the exact top left corner (0,0) for the mouse offset to work properly (try starting the box off in the middle of the screen, it jumps around just like when there was no offset!).
If you want the box to start in the middle, place it in the top corner and then also give it an initial translation as to appear in the middle.
Hi,
Awesome Job! Great thanks for the example. Can you please let us know how did you manage to create such drag and drop effect in physics example.
Thanks,
Sundeep.
[...] 2.) Click the Move button to enable the move tool. Once you have clicked this the cursor becomes an Hand indicating you can click and drag on the screen to move the objects around. The technique I’m using for this is similar to a previous post – Drag and drop Silverlight example. [...]