Silverlight 4 Toolkit Drag & Drop

So I have been working with the Toolkit’s new Drag & Drop features. And design pattern aside, it’s pretty cool. I’m not sure why they had to implement the drag drop target as an actual set of controls vs attached behaviors but beggars can’t be choosers.

<toolkit:ListBoxDragDropTarget 
    AllowDrop="true" 
    >
    <i:Interaction.Triggers>
        <i:EventTrigger EventName="Drop">
            <mvvm:InvokeViewModelMethod MethodName="DropSOI" />
        </i:EventTrigger>
    </i:Interaction.Triggers>
    <ListBox 
        ItemsSource="{Binding AllCoverages}" 
        >
    </ListBox>
</toolkit:ListBoxDragDropTarget>

 

So after wiring up a ListBoxDragDropTarget and handling it’s Drop event (see above) I am finally receiving a data object. I thought that the “DataObject” on the Drop Event arguments would be the originating content type “SOI”.

public void DropSOI(Microsoft.Windows.DragEventArgs e)
{
    object data = e.Data.GetData(typeof(SOI));
}

But as it turns out it’s far from it. The above code just does work, it returns null. It’s because I didn’t case it to the correct type. I needed to cast it to an ItemDragEventArgs. This class has an object call data that I can then use.

image

It turns out even after getting the ItemDragEventArgs object back I need to do a bit more digging before I can get at my true content. Below is the code that I used to finally get at my content:

public void DropSOI(Microsoft.Windows.DragEventArgs e)
{
    ItemDragEventArgs args = e.Data.GetData(typeof(ItemDragEventArgs)) as ItemDragEventArgs;
    var collection = args.Data as SelectionCollection;
    var selection = collection[0] as Selection;
    var data = selection.Item as SOI;
}

3 thoughts on “Silverlight 4 Toolkit Drag & Drop

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s