Cancel DraggingTool

Hi,
I have a treeview as my source for drag’n’drop to create nodes on the diagram. I implemented a CustomDraggingTool too.

All works well.

Now I have a user, he starts dragging from the treeview, moves the new node in the diagram, but before dropping in the diagram he moves the node back to the treeview and release the mousebutton. I’ve tried many ugly things to handle this, getting unwanted dependencies in the View with the treeview.

Is there a way the customdraggingtool can be informed that the drag is canceled or that the drop happened on another control?

What methods have you tried overriding on your CustomDraggingTool?

DraggingTool.DoDragEnter, DraggingTool.DoDragLeave, and DraggingTool.DoQueryContinueDrag may be of interest to you.

DragEnter and DragLeave.
But these are fired continously. I read about that. It seems to be a problem that the mouse pointer is pointing to the node and than on the diagram than on the node again and so on .net - WPF Drag Drop - When Does DragLeave Fire? - Stack Overflow.
But even if these come only one time each for the diagram (one time on enter and one time on leave - if going back to the treeview) the diagram is showing a “ghost” node until I move the mouse over the diagram or calling DoCancel over some dependencies.

DoQueryContinueDrag was not fired. The Drag start from the TreeView maybe that’s the reason.

I assume you already have some code to initiate the drag-and-drop from the TreeView, something like:

    private void myTreeView_MouseMove(object sender, MouseEventArgs e) {
      if (e.LeftButton == MouseButtonState.Pressed && myTreeView.SelectedItem != null) {
        DragDrop.DoDragDrop(myTreeView, myTreeView.SelectedValue, DragDropEffects.Copy);
      }
    }

You cannot override DraggingTool.DoDragLeave, because of repeated “drag leave” events. Those repeated drag-leave events occur because the mouse is “leaving” the Diagram to go “into” a Node or a Link. The problem is that at the time of the “drag leave” event we do not know where it is going, otherwise we could decide whether or not to stop the tool based on whether we think it is staying within the Diagram or going outside of the Diagram.

But you could put events on the rest of the window. For example, in your TreeView you could add another handler:

    MouseMove="myTreeView_MouseMove" DragEnter="myTreeView_DragEnter" AllowDrop="True"

implemented by:

    private void myTreeView_DragEnter(object sender, DragEventArgs e) {
      if (myDiagram.DraggingTool.CopiedParts != null) {
        myDiagram.DraggingTool.DoStop();
      }
    }

Hi Walter,
yes this could work. But then the MVVM-View containing the TreeView has to know about a diagram (that’s I meant with unwanted dependencies).
I want to use this view in many application, most of them have no diagram-control.

Could you establish the DragEnter event handler on the whole window?

Yes, that is possible.