Posted by Boyan Mihailov as Silverlight
In some occasions you will need to drag & drop items between different objects in your Silverlight application. Although in Windows Forms is very easy to achieve this goal (because you have the events DragEnter, DragDrop), in Silverlight you can do the same with little tricks. In this example I will show you how to do drag & drop over a canvas.
We are going to create a project, called Freedom in the Sky. We have a custom control, called Bird, which we want to drag & drop over a canvas. Here is our custom control.
<UserControl x:Class="DragAndDropProject.Bird" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" MouseLeftButtonDown="Bird_MouseLeftButtonDown" MouseLeftButtonUp="Bird_MouseLeftButtonUp" MouseMove="Bird_MouseMove" Width="100" Height="112"> <Grid x:Name="LayoutRoot" Background="Transparent"> <Image Source="Images/pigeon.png" /> </Grid> </UserControl>
It is nothing more than a transparent image. The real work is done in the code-behind. As you see we are going to use MouseLeftButtonDown, MouseLeftButtonUp and MouseMove events:
FrameworkElement item = sender as FrameworkElement;
if (this.isMouseCaptured)
{
// Calculate the current position of the object
double deltaV = e.GetPosition(null).Y - this.mousePosition.Y;
double deltaH = e.GetPosition(null).X - this.mousePosition.X;
double newTop = deltaV + (double)item.GetValue(Canvas.TopProperty);
double newLeft = deltaH + (double)item.GetValue(Canvas.LeftProperty);
Canvas parent = item.Parent as Canvas;
if ((newTop + this.Height <= parent.ActualHeight) && (newTop >= 0) &amp;&amp;
(newLeft + this.Width <= parent.ActualWidth) && (newLeft >= 0))
{
item.SetValue(Canvas.TopProperty, newTop);
item.SetValue(Canvas.LeftProperty, newLeft);
}
// Update position global variable
this.mousePosition = e.GetPosition(null);
}
Here isMouseCaptured indicates whether we are doing drag & drop; and mousePosition is the position of the mouse when the user has pressed the left button.
[ Online Demo ] [ Source Code ]
RSS feed for comments on this post · TrackBack URI
Leave a reply