Introducing DotNetFunda.com on mobile http://m.dotnetfunda.com ! Be with DotNetFunda.com on the go !
Go to DotNetFunda.com
Twitter TwitterLinkedIn
YouTubeGoogle
 Online : 4930 |  Welcome, Guest!   Register  Login
Home > Articles > Silverlight > Silverlight: Drag And Drop ListBoxItem to Canvas (using Telerik Control)

Silverlight: Drag And Drop ListBoxItem to Canvas (using Telerik Control)

Article posted by Kunal2383 on 3/4/2010 | Views: 7623 | Category: Silverlight | Level: Beginner red flag


While surfing thru different forum I noticed that, lots of people are actually facing issues while trying to implement the drag and drop feature. The main problem arises while trying to drag from a ListBox to a panel like canvas. In this post, I will go thru the steps to demonstrate such feature.

Download


 Download source code for Silverlight: Drag And Drop ListBoxItem to Canvas (using Telerik Control)


Introduction
While surfing thru different forum I noticed that, lots of people are actually facing issues while trying to implement the drag and drop feature. The main problem arises while trying to drag from a ListBox to a panel like canvas. In this post, I will go thru the steps to demonstrate such feature.

Prerequisite

Here I will use Telerik control to give out the demonstration. You can download the trial version of the dlls from Telerik Silverlight Control Page. I have implemented the demo using Silverlight 4 Beta 1. The same thing is also possible in earlier version of Silverlight. You can download Silverlight SDK from Silverlight Site. To develop apps in Silverlight 4 you must need Visual Studio 2010 Beta 2 which you can download from Microsoft site.


Code Walk through

So, lets go for implementing the same. Create a Silverlight project. Lets create a ListBox and a Canvas inside the LayoutRoot:

<Grid x:Name="LayoutRoot" Background="White">
<
Grid.ColumnDefinitions>
<
ColumnDefinition Width="150"/>
<
ColumnDefinition Width="*"/>
</
Grid.ColumnDefinitions>
<
ListBox x:Name="lstBox" Margin="10" Grid.Column="0"/>
<
Canvas x:Name="cnvDropBox" Background="Yellow" Margin="10" Grid.Column="1"/>
</
Grid>

Now in the code behind, we have to register the Drag and Drop events for the ListBox & Canvas. Use RadDragAndDropManager class to register the same. 
RadDragAndDropManager.AddDragInfoHandler(lstBox, OnDragInfo);
RadDragAndDropManager.AddDragQueryHandler(lstBox, OnDragQuery);
RadDragAndDropManager.AddDropInfoHandler(cnvDropBox, OnDropInfo);
RadDragAndDropManager.AddDropQueryHandler(cnvDropBox, OnDropQuery);

RadDragAndDropManager.SetAllowDrop(cnvDropBox, true);

The implementation of the events will be as below: 

private void OnDragQuery(object sender, DragDropQueryEventArgs e)
{
if (e.Options.Status == DragStatus.DragQuery)
{
var draggedListBoxItem = e.Options.Source as Image;
e.Options.DragCue = draggedListBoxItem.Source;
e.Options.Payload = draggedListBoxItem.Source;
}

e.QueryResult = true;
e.Handled = true;
}

private void OnDragInfo(object sender, DragDropEventArgs e)
{
if (e.Options.Status == DragStatus.DragComplete)
{
// comment this block if you are going to clone
lstBox.Items.Remove(e.Options.Source);
}
}

private void OnDropInfo(object sender, DragDropEventArgs e)
{
var droppablePanel = e.Options.Destination;

if (e.Options.Status == DragStatus.DropComplete && droppablePanel is Canvas)
{
FrameworkElement dragableControl = null;
Point desiredPosition = new Point();
Point currentDragPoint = e.Options.CurrentDragPoint;
Point canvasPosition = cnvDropBox.TransformToVisual(null).Transform(new Point());

if (e.Options.Source is Image)
{
// create the new instance & update the necessary properties
// this step is require if you are going to make a clone
Image tempDragableControl = e.Options.Source as Image;
dragableControl = new Image() { Source = tempDragableControl.Source };
cnvDropBox.Children.Add(dragableControl);
}

desiredPosition.X = currentDragPoint.X - canvasPosition.X;
desiredPosition.Y = currentDragPoint.Y - canvasPosition.Y;
dragableControl.SetValue(Canvas.LeftProperty, desiredPosition.X);
dragableControl.SetValue(Canvas.TopProperty, desiredPosition.Y);
}
}


private void OnDropQuery(object sender, DragDropQueryEventArgs e)
{
var droppablePanel = e.Options.Destination;

if (e.Options.Status == DragStatus.DropDestinationQuery && droppablePanel is Canvas)
{
e.QueryResult = true;
e.Handled = true;
}
}

As I am using Image inside the ListBoxItem, hence OnDragQuery I am setting the Source as an Image to the DragCue & PayLoad properties. OnDragInfo I am removing item from the ListBox. If you don’t want to remove the dragged image from the ListBox then just remove that line. OnDropInfo I am just placing the Image to the appropriate position which we will get as CurrentDragPoint in the DragDropEventArgs


Conclusion

This is a sample demonstration. So, you can now explore it more to fulfil your requirement.


Download Sample Application:  Drag And Drop ListBoxItem to Canvas Demo



If you like this article, subscribe to our RSS Feed. You can also subscribe via email to our Interview Questions, Codes and Forums section.

Page copy protected against web site content infringement by Copyscape
Found interesting? Add this to:



Please Sign In to vote for this post.

Experience:3 year(s)
Home page:http://www.kunal-chowdhury.com
Member since:Monday, March 01, 2010
Level:Starter
Status: [Member]
Biography:He is currently working as a Silverlight application developer. Has a very good skill over C#, XAML, Silverlight & WPF. He has a good working experience in Windows 7 application (including Multitouch) development.

During his professional career he worked in various technologies & delivered quality output. He never hesitates to take up challenges & work on the latest technologies in Microsoft platform.

He attended various software development competition & achieved different awards.

He is presently focusing on the RIA (Silverlight & WPF) & willing to become a Technology Specialist in Microsoft platform. Learning newer things, Blog posting & helping others in forums is one of his regular activity.

Specialties: Silverlight Application Development, WPF Application Development, Windows 7 Application Development
>> Write Response - Respond to this post and get points
Related Posts

In this article I will not be binding the controls with the data from Database. My example is a very simple example, like a hello world type of Example. In this article I am going to bind the individual controls from a property value. To follow my example you need Expression Blend.

Silverlight is a Cross-Browser, Cross-Platform plug-in from Microsoft. It supports many features that cannot be done in a normal browser it helps Web Applications to give Rich User Experience using WPF and uses client GPUs to provide this. It also enhances the productivity with the support of .NET APIs, Visual Studio and Expression Blend.

This Article will help to start using Services in Silverlight 3.0. We will see how we do Data transfer operations using ADO.NET Data Services. Please refer to my previous articles on Sliverlight Part1, Part2 to get started

This article will talk about 4 simple steps which will assist you to consume WCF service in a Silverlight application. It also had a simple sample source code which demonstrates all the 4 steps practically.

In this article, we are going to learn about Different types of Data Binding Techniques in Silverlight and how to validate Data in SilverLight.

More ...
About Us | Contact Us | The Team | Advertise | Software Development | Write for us | Testimonials | Privacy Policy | Terms of Use | Link Exchange | Members | Go Top
General Notice: If you found plagiarised (copied) contents on this page, please let us know the original source along with your correct email id (to communicate) for further action.
Copyright © DotNetFunda.Com. All Rights Reserved. Copying or mimicking the site design and layout is prohibited. Logos, company names used here if any are only for reference purposes and they may be respective owner's right or trademarks. | 5/21/2012 7:56:19 AM