How to Use the Mouse Middle Button for Pan and Middle Button Scroll for Zoom in WPF Diagram (SfDiagram)
In the WPF Diagram (SfDiagram), you can customize the mouse middle button and mouse scroll actions by using the PreviewMouseMove event. We have prepared a sample for panning the diagram by pressing the middle mouse button, and you can zoom in or zoom out the diagram by scrolling the mouse wheel.
Code Snippet:
//Adding the previewMouseMove Event.
diagram.PreviewMouseMove += Diagram_PreviewMouseMove;
//Method for executing the PreviewMouseMove Event.
private void Diagram_PreviewMouseMove(object sender, MouseEventArgs e)
{
//Getting the current mouse point position of the diagram.
Point currentMousePoint = e.GetPosition(diagram.Page);
Point panDelta = new Point(currentMousePoint.X - InitialLocation.X, currentMousePoint.Y - InitialLocation.Y);
if (e.MiddleButton == MouseButtonState.Pressed)
{
//Disabling the intelli mouse wheel action when pressing the middle button.
e.Handled = true;
//Getting the current zoom scale value.
double currentZoomLevel = diagram.ScrollSettings.ScrollInfo.CurrentZoom;
//Translating the pan delta changes to current zoom scale value.
panDelta.X = panDelta.X * currentZoomLevel;
panDelta.Y = panDelta.Y * currentZoomLevel;
//Setting the hand cursor for panning action.
Mouse.SetCursor(Cursors.Hand);
//Executing the panning process when the middle mouse button moves.
(diagram.Info as IGraphInfo).Commands.Zoom.Execute(new ZoomPositionParameter() { ZoomCommand = ZoomCommand.Pan, PanDelta = panDelta });
}
}
Conclusion:
I hope you enjoyed learning how to use the mouse middle button for pan and the middle button scroll for zoom in the WPF Diagram (SfDiagram).
Refer to our WPF Diagram feature tour page to learn about its other groundbreaking feature representations and documentation, and how to quickly get started for configuration specifications.
For current customers, you can check out our components from the License and Downloads page. If you are new to Syncfusion®, try our 30-day free trial to check out our other controls.
Please let us know in the following comments section if you have any queries or require clarifications. You can also contact us through our support forums, Direct-Trac, or feedback portal. We are always happy to assist you!