Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Wpf dragdrop, how to visually reject the drop during the drag?

During a drag in Wpf, how can the mouse cursor (or perhaps using an adorner) be changed to indicate that the droptarget will not accept the dragged item?

I've tried to set e.Effects = DragDropEffects.None during the DragEnter event but this isn't working and I suspect that I've misunderstood what that feature should be used for. I've tried using the GiveFeedback event but don't see how the droptarget can influence it.

like image 270
awx Avatar asked Nov 02 '09 05:11

awx


1 Answers

Just setting the DragDropEffects in DragEnter of the drop target should work. Is your DragEnter even getting called. Have you set AllowDrop on the drop target control?

This is the sequence of events during a drag & drop in WPF (taken from MSDN) which might help work out what's going on...

  1. Dragging is initiated by calling the DoDragDrop method for the source control.

    The DoDragDrop method takes two parameters: * data, specifying the data to pass * allowedEffects, specifying which operations (copying and/or moving) are allowed

    A new DataObject object is automatically created.

  2. This in turn raises the GiveFeedback event. In most cases you do not need to worry about the GiveFeedback event, but if you wanted to display a custom mouse pointer during the drag, this is where you would add your code.
  3. Any control with its AllowDrop property set to True is a potential drop target. The AllowDrop property can be set in the Properties window at design time, or programmatically in the Form_Load event.
  4. As the mouse passes over each control, the DragEnter event for that control is raised. The GetDataPresent method is used to make sure that the format of the data is appropriate to the target control, and the Effect property is used to display the appropriate mouse pointer.
  5. If the user releases the mouse button over a valid drop target, the DragDrop event is raised. Code in the DragDrop event handler extracts the data from the DataObject object and displays it in the target control.
like image 190
IanR Avatar answered Oct 03 '22 10:10

IanR