One of the great new features of Silverlight 4 is drag-drop. This makes it possible to drag files from your OS onto the Silverlight application. In this tutorial we’ll create a ListBox and make it possible to drop images from the desktop onto the application. The images will then appear in the ListBox.
First steps
First of all we’ll create a “normal” Silverlight 4 App + Website using Visual Studio 2010 or Expression Blend. Next we need a ListBox to show the images when they are dropped on it. Make sure your XAML looks similar to this:
<UserControl x:Class="DragDrop.MainPage"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
mc:Ignorable="d"
d:DesignHeight="328" d:DesignWidth="514"> <Grid x:Name="LayoutRoot" Background="White"> <ListBox Height="Auto" HorizontalAlignment="Left" Name="listBox1" VerticalAlignment="Stretch" Width="200" /> </Grid> </UserControl> Hooking it up
Next step is to create an eventhandler for the drop event. But first we need to make sure that it is allowed to drop objects onto the ListBox. This is done by setting the AllowDrop property of the ListBox to true. So with the addition of that ...