[Solved] Pan tool


I've a button to enable Pan mode for the diagram. When clicked the diagram's currentTool was set to the diagram's panTool. But, the problem with this was "one-time-use" i.e., once the user clicks on the diagram and releases the mouse button the currentTool is set to dragging tool. So, the user can't use the pan tool multiple times without having to click the pan button everytime.

To overcome this, I followed the below steps:

To enable Pan mode:

  • Store drag & dragSelection tool to a variable
  • Set diagram’s DraggingTool & DragSelectingTool to null. By doing this the pan tool can be used multiple times.

To disable Pan mode:

  • Store pan tool to a variable
  • Set DraggingTool & DragSelectingTool to value that was store previously
  • Set PanningTool to null. This enables the selection of DraggingTool

This worked fine in WPF. But, it is not working in Silverlight. The source of error is while disabling the pan tool. When I try to assign the DraggingTool with the previously stored value, I’m getting the error “Element is already the child of another element.”

Below is the code snippet:

private void SetPanMode()
    if (PanMode == true)
        if (defaultDragTool == null) defaultDragTool = taskDiagram.DraggingTool;
        if (defaultSelectTool == null) defaultSelectTool = taskDiagram.DragSelectingTool;

        taskDiagram.DraggingTool = null;
        taskDiagram.DragSelectingTool = null;

        if (taskDiagram.PanningTool == null && defaultPanTool != null)
            taskDiagram.PanningTool = defaultPanTool;
        if (defaultPanTool == null) defaultPanTool = taskDiagram.PanningTool;

        taskDiagram.DraggingTool = defaultDragTool; <font color="#FF0000"><font color="#000000">//</font><b>Source of error</b></font>
        taskDiagram.DragSelectingTool = defaultSelectTool;
        taskDiagram.PanningTool = null;

Version: GoWPF, GoSilverlight


The normal way to do that “mode” switch is to set DiagramTool.MouseEnabled, instead of replacing the Diagram property with null or with the original tool.

Another possibility is to override PanningTool.DoMouseUp not to call StopTool(). That way the PanningTool remains current “forever”, or until the user hits Escape or invokes some command to replace the Diagram.CurrentTool.

Finally a third possibility is to remove the Diagram.DragSelectingTool (if you don’t need that functionality). That way users can drag Nodes but dragging in the background pans. So there’s no “mode” switch needed.

Thanks Walter.

I used the .MouseEnabled. Below is the updated code snippet:
private void SetPanMode()
    taskDiagram.DraggingTool.MouseEnabled = taskDiagram.DragSelectingTool.MouseEnabled = !PanMode;
    taskDiagram.PanningTool.MouseEnabled = PanMode;

I disable DraggingTool so that when in pan mode user can click & drag anywhere on the diagram (even on the nodes) to pan the diagram.

Phew… the code got reduced very much & is very clean now.

Thanks a lot Smile