/// <summary> /// Delegate called when the mouse move on the working canvas. /// </summary> /// <param name="pSender">The event sender.</param> /// <param name="pEventArgs">The event arguments.</param> private void OnWorkingCanvasMouseMove(object pSender, MouseEventArgs pEventArgs) { if (pEventArgs.LeftButton == MouseButtonState.Pressed) { // Create a path according to the source and the end. this.ConnectingLine.To = pEventArgs.GetPosition(this.WorkingCanvas); // Updating the cursor. this.mTargetConnector = this.TryHitInputConnector(); if (this.mTargetConnector != null) { PortViewModel lTargetViewModel = this.mTargetConnector.ParentPort.Content as PortViewModel; PortViewModel lSourceViewModel = this.mSourceConnector.ParentPort.Content as PortViewModel; if (lSourceViewModel.CanBeConnectedTo(lTargetViewModel)) { this.WorkingCanvas.Cursor = Cursors.Cross; } else { this.WorkingCanvas.Cursor = Cursors.No; this.mTargetConnector = null; } } else { this.WorkingCanvas.Cursor = Cursors.Cross; } } }
/// <summary> /// This method is called when a mouse button up occured on the adorner. /// </summary> /// <param name="pEventArgs">The event arguments</param> protected override void OnMouseUp(MouseButtonEventArgs pEventArgs) { // Release the mouse capture. if (this.IsMouseCaptured) { this.ReleaseMouseCapture(); } // Getting the position. Point lHitPoint = pEventArgs.GetPosition(this); AdornerLayeredCanvas lParentCanvas = this.AdornedElement as AdornerLayeredCanvas; if (lParentCanvas != null) { // Remove the adorner. AdornerLayer lLayer = lParentCanvas.AdornerLayer; if (lLayer != null) { lLayer.Remove(this); } // Hitting the target connector. InputConnector lTargetConnector = lParentCanvas.HitControl <InputConnector>(lHitPoint); if (lTargetConnector != null) { GraphViewModel lGraphViewModel = lParentCanvas.DataContext as GraphViewModel; if (lGraphViewModel != null) { PortViewModel lTargetViewModel = lTargetConnector.ParentPort.Content as PortViewModel; PortViewModel lSourceViewModel = this.mSourceConnector.ParentPort.Content as PortViewModel; if (lTargetViewModel != null && lSourceViewModel.CanBeConnectedTo(lTargetViewModel)) { ConnectionViewModel lConnectionViewModel = new ConnectionViewModel(); lConnectionViewModel.Output = lSourceViewModel; lConnectionViewModel.Input = lTargetViewModel; lGraphViewModel.AddConnection(lConnectionViewModel); } } } } }