views:

65

answers:

1

I am really struggling to get a simple drag and drop sample working in Silverlight 4.

Here's what I have:

XAML

<UserControl x:Class="TestDragDrop.MainPage" Width="350" Height="200"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"&gt;
    <Grid Background="White">
        <Rectangle Margin="50,50,200,50" Fill="Orange" MouseLeftButtonDown="r1_MouseLeftButtonDown" />
        <Rectangle Margin="200,50,50,50" Fill="Orange" AllowDrop="true" Drop="r2_Drop" />
    </Grid>
</UserControl>

Code-Behind

public partial class MainPage : UserControl
{
    public MainPage()
    {
        InitializeComponent();
    }

    private void r1_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
    {
        DragDrop.DoDragDrop((Rectangle)sender, "test data", DragDropEffects.All, DragDropKeyStates.LeftMouseButton);
    }

    private void r2_Drop(object sender, System.Windows.DragEventArgs e)
    {
        MessageBox.Show("Drop: " + e.Data.ToString());
    }
}

The DragDrop.DragDropCompleted event does fire, however the sender parameter is always null and the event args do not really help me finding more about the event.

I have also tried to use a custom control implementing IAcceptDrop with no luck.

Also, when I start the drag operation, I have no visual feedback that something is happening (no change in the cursor or anything). Is there something wrong?

All the samples I have found use the DragDropTargets. Is my only resort to implement a DragDropTarget for the particular type of controls I want to use?

+1  A: 

Silverlight 4's drag and drop feature was targeted at a single scenario: dragging a file from your computer onto a Silverlight application. Anything beyond that at you need to use the Toolkit's DragDropTarget controls. The description of the drag/drop feature from Tim Heuer: http://timheuer.com/blog/archive/2009/11/18/whats-new-in-silverlight-4-complete-guide-new-features.aspx

For some scenarios, you may have wanted to be able to drag a file from your desktop or file explorer on to your Silverlight application. By enabling the AllowDrop attribute on UIElement in this release, you can now accommodate those scenarios.

In other words, they did not plan on giving you the ability to actually create your own drag operations in code.

Most, if not all, of the "Limitations" section from this URL still apply to SL4 RTW: http://www.ningzhang.org/2009/11/28/silverlight-drag-and-drop-api/

  • there is no drop source support (QueryContinueDrag & GiveFeedback).
  • there is no DragDropEffects or DragDropKeyStates in DragEventArgs.
  • there is no visual for the dragged object or DragDropEffects.
  • only file drag and drop is supported: all drop target events fire only when files are being dragged and dropped. IDataObject, DataObject and DragEventArgs.Data support only one format: “FileDrop”, and the data is of type FileInfo[].
  • most of IDataObject methods throw NotImplementedException.
Adam Sills
Thanks for your answer. I am using the DragDrop.DoDragDrop() method from the Toolkit. Previously there was an attached AllowDrop property from the Toolkit as well but this as been removed in SL4. I take it that the framework property has replaced it?
Xavier Poinas
Yes, you can use the framework AllowDrop property.
Adam Sills