/// <summary> /// Event raised when the user starts to drag a connector. /// </summary> private void ConnectorItem_DragStarted(object source, ConnectorItemDragStartedEventArgs e) { Focus(); e.Handled = true; IsDragging = true; IsNotDragging = false; IsDraggingConnection = true; IsNotDraggingConnection = false; _draggedOutConnectorItem = (ConnectorItem)e.OriginalSource; var nodeItem = _draggedOutConnectorItem.ParentNodeItem; _draggedOutNodeDataContext = nodeItem.DataContext != null ? nodeItem.DataContext : nodeItem; _draggedOutConnectorDataContext = _draggedOutConnectorItem.DataContext != null ? _draggedOutConnectorItem.DataContext : _draggedOutConnectorItem; // // Raise an event so that application code can create a connection and // add it to the view-model. // var eventArgs = new ConnectionDragStartedEventArgs(ConnectionDragStartedEvent, this, _draggedOutNodeDataContext, _draggedOutConnectorDataContext); RaiseEvent(eventArgs); // // Retrieve the the view-model object for the connection was created by application code. // _draggingConnectionDataContext = eventArgs.Connection; if (_draggingConnectionDataContext == null) { // // Application code didn't create any connection. // e.Cancel = true; } }
/// <summary> /// The mouse cursor has been moved. /// </summary> protected override void OnMouseMove(MouseEventArgs e) { base.OnMouseMove(e); if (_isDragging) { // // Raise the event to notify that dragging is in progress. // var curMousePoint = e.GetPosition(ParentNetworkView); var offset = curMousePoint - _lastMousePoint; if (Math.Abs(offset.X) > Epsilon && Math.Abs(offset.Y) > Epsilon) { _lastMousePoint = curMousePoint; RaiseEvent(new ConnectorItemDraggingEventArgs(ConnectorDraggingEvent, this, offset.X, offset.Y)); } e.Handled = true; } else if (_isLeftMouseDown) { if (ParentNetworkView != null && ParentNetworkView.EnableConnectionDragging) { // // The user is left-dragging the connector and connection dragging is enabled, // but don't initiate the drag operation until // the mouse cursor has moved more than the threshold distance. // var curMousePoint = e.GetPosition(ParentNetworkView); var dragDelta = curMousePoint - _lastMousePoint; var dragDistance = Math.Abs(dragDelta.Length); if (dragDistance > DragThreshold) { // // When the mouse has been dragged more than the threshold value commence dragging the node. // // // Raise an event to notify that that dragging has commenced. // var eventArgs = new ConnectorItemDragStartedEventArgs(ConnectorDragStartedEvent, this); RaiseEvent(eventArgs); if (eventArgs.Cancel) { // // Handler of the event disallowed dragging of the node. // _isLeftMouseDown = false; return; } _isDragging = true; CaptureMouse(); e.Handled = true; } } } }