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?