Drag and Drop Files in Silverlight 4


Introduction

In Silverlight 4, the plugin itself supports Drag and Drop events. In this article we will see how can we use the DropTarget to capture files from local system.

Creating Silverlight Project

Fire up Visual Studio 2010 and create a new Silverlight Application Project. Name it as DragAndDropSample.

1.gif
 
Let's have a ListBox and a Border which can fulfill our requirement.

2.gif
 
As you see in above image, the RightSide pane is the Drop Target and the LeftPane will Populate the list of items dropped.

Let's have a class that will help us binding the files into listbox.

3.gif
 
Now let's change the default ItemTemplate of the ListBox.

4.gif
 
Now Let's have the Drop event of the Border so that we can capture the files.

5.gif
 
Now that everything is done in XAML, let's go to the code behind and write code into the DropHere event.
 
 6.gif
7.gif

8.gif
 
 
Now we are good to go. Let's have some files from the local file system and drop onto the Drop Area.

9.gif

10.gif
 
That's it.

Hope this article helps.

Up Next
    Ebook Download
    View all
    Learn
    View all