I am using the RadDiagram component inside a WPF application for a sketching software. The default behavior of the RadDiagram is to enable movement of the diagram with the Ctrl + Left click and drag shortcut. I want to enable the program to also work with Middle mouse button click and drag. Currently, I am using custom RadDiagram tools, which derive from their base classes. For example, MyPanningTool:
I've tried to capture the Middle mouse click event on the RadDiagram and transfer them to the Panning tool Mouse click and and Move events, but this doesn't seem to do the trick.
using System;
using System.Windows.Input;
using XXX.Core.Models.EventArgs;
using Telerik.Windows.Diagrams.Core;
namespace XXX.XXX.Presentation.Tools
{
internal class MyPanningTool : PanningTool
{
private const string PanningToolName = "Panning Tool";
private SelectionMode _selectionMode;
private ToolService _toolService;
public MyPanningTool()
{
Cursor = DiagramCursors.Dragging;
}
private void ActivatePanningTool()
{
_toolService ??= Graph.ServiceLocator.GetService<IToolService>() as ToolService;
_toolService?.ActivateTool(PanningToolName);
}
private void ActivatePointerTool()
{
_toolService ??= Graph.ServiceLocator.GetService<IToolService>() as ToolService;
_toolService?.ActivateTool("Pointer Tool");
}
public override bool KeyDown(KeyArgs key)
{
if (!IsActive) return false;
base.KeyDown(key);
if (key.Key != Key.Enter && key.Key != Key.Escape && !ToolService.IsControlDown)
{
ActivatePanningTool();
return false;
}
return false;
}
/// <inheritdoc />
public override bool MouseDown(PointerArgs e)
{
if (IsActive)
{
_selectionMode = Graph.SelectionMode;
Graph.SelectionMode = SelectionMode.None;
}
if (ToolService.IsControlDown || Mouse.MiddleButton == MouseButtonState.Pressed)
{
//ActivateTool();
//ActivatePanningTool();
return base.MouseDown(e);
}
return IsActive && base.MouseDown(e);
}
/// <inheritdoc />
public override bool MouseMove(PointerArgs e)
{
if (ToolService.IsControlDown || Mouse.MiddleButton == MouseButtonState.Pressed)
{
return base.MouseMove(e);
}
return IsActive && base.MouseMove(e);
}
/// <inheritdoc />
public override bool MouseUp(PointerArgs e)
{
if (!IsActive) return false;
Graph.SelectionMode = _selectionMode;
try
{
base.MouseUp(e);
}
catch
{
// ignored
}
if (ToolService.IsControlDown || Mouse.MiddleButton == MouseButtonState.Pressed) return base.MouseUp(e);
ActivatePointerTool();
ToolChangedEvent?.Invoke(null, new ToolChangedEventArgs("Pointer Tool"));
return true;
}
public static event EventHandler<ToolChangedEventArgs> ToolChangedEvent;
}
}
I've tried to capture the Middle mouse click event on the RadDiagram and transfer them to the Panning tool Mouse click and and Move events, but this doesn't seem to do the trick.