I'm trying to write some generic code for handling drops in WPF drop targets. AllowDrop
is set to true, and I've hooked onto DragEnter
, DragOver
, DragLeave
, & Drop
on the drop target UIElement. Using the bubbling events enables nesting of drop targets.
Note: I have no access to the drag source - this is inter-application drag & drop.
If I have some UI cleanup to perform at the end of a potential drop and the user presses Esc to cancel the drop, the drop target never seems to gets a specific event that I can differentiate from all the others. Drop is easy, but what indicates a cancel?
The problem I have is this:
DragLeave
is a bubbling routed event.e.OriginalSource
is always set for this event (and the corresponding Preview) via hittesting.- The target is an
ItemsControl
(Listbox is what I've currently been testing with).
As I drag over my intended drop target, I get loads of DragLeave
events from the child visuals within the target. I never get any from the target itself. Grids, rectangles, borders, text blocks, they all happily send me DragLeave
, but none from the actual ItemsControl
I'm connected up to. I thought it might be a hit-testing problem, but I've set the Background of the ItemsControl
to a colour, and it makes no difference.
What am I missing? How am I supposed to determine that a drop operation has definitely finished?
(The actual problem I'm trying to solve is that I'm implementing some custom dragging behaviour in a TreeView that expands folders when you hover over them, and cancels timers & undoes the expansion when the drop is finished, and more to come, but I can't even get the events to fire sensibly for a ListBox).