Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Silverlight DragDrop.DoDragDrop()

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">
    <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?

like image 452
Xavier Poinas Avatar asked Oct 06 '10 01:10

Xavier Poinas


1 Answers

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.
like image 79
Adam Sills Avatar answered Sep 23 '22 06:09

Adam Sills