There is a WrapPanel filled with a Label, the resulting field is 10x10. Label created dynamically, two events cling to them

la.Drop += lbl_Drop; la.MouseDown += lbl_MouseDown; private void lbl_MouseDown(object sender, MouseButtonEventArgs e) { Label lbl = (Label)sender; DragDrop.DoDragDrop(lbl, lbl.Content, DragDropEffects.All); } private void lbl_Drop(object sender, DragEventArgs e) { ((Label)sender).Background = new SolidColorBrush(Colors.Blue); } 

When you try to drag it, an icon appears that indicates that you can not do this, what's the problem?

  • And why should it work at all? You move the string, why should this move the control? - VladD
  • Here is the code: www.stackoverflow.com/a/444924/10105 , he can move even between windows. No DoDragDrop and similar heavy artillery are needed. - VladD

1 answer 1

In order for the drop to work, you need to enable AllowDrop on the object being dragged and the source. From the description of the Drop event:

For the Drop event to occur and trigger the handler, the AllowDrop value for the UIElement, which is the target of the drag, as well as the source of the drag action, must be true.

And Drop must be processed on the element that accepts, and not on the dragged one. From the same article, the description of the Drop event:

Occurs when an input system reports a main drag event in which a given item is the destination.