/// <summary> /// Event raised when the user has finished dragging a connector. /// </summary> private void ConnectorItem_DragCompleted(object source, ConnectorItemDragCompletedEventArgs e) { e.Handled = true; Trace.Assert((ConnectorItem)e.OriginalSource == this.draggedOutConnectorItem); Point mousePoint = Mouse.GetPosition(this); // // Figure out if the end of the connection was dropped on a connector. // ConnectorItem connectorDraggedOver = null; object connectorDataContextDraggedOver = null; DetermineConnectorItemDraggedOver(mousePoint, out connectorDraggedOver, out connectorDataContextDraggedOver); // // Now that connection dragging has completed, don't any feedback adorner. // ClearFeedbackAdorner(); // // Raise an event to inform application code that connection dragging is complete. // The application code can determine if the connection between the two connectors // is valid and if so it is free to make the appropriate connection in the view-model. // RaiseEvent(new ConnectionDragCompletedEventArgs(ConnectionDragCompletedEvent, this, this.draggedOutNodeDataContext, this.draggingConnectionDataContext, this.draggedOutConnectorDataContext, connectorDataContextDraggedOver)); this.IsDragging = false; this.IsNotDragging = true; this.IsDraggingExecutionConnection = false; this.IsDraggingConnection = false; this.IsNotDraggingExecutionConnection = true; this.IsNotDraggingConnection = true; this.draggedOutExecutionConnectorDataContext = null; this.draggedOutConnectorDataContext = null; this.draggedOutNodeDataContext = null; this.draggedOutExecutionConnectorItem = null; this.draggedOutConnectorItem = null; this.draggingExecutionConnectionDataContext = null; this.draggingConnectionDataContext = null; }
/// <summary> /// This function does a hit test to determine which connector, if any, is under 'hitPoint'. /// </summary> private bool DetermineConnectorItemDraggedOver(Point hitPoint, out ConnectorItem connectorItemDraggedOver, out object connectorDataContextDraggedOver) { connectorItemDraggedOver = null; connectorDataContextDraggedOver = null; // // Run a hit test // HitTestResult result = null; VisualTreeHelper.HitTest(nodeItemsControl, null, // // Result callback delegate. // This method is called when we have a result. // delegate(HitTestResult hitTestResult) { result = hitTestResult; return HitTestResultBehavior.Stop; }, new PointHitTestParameters(hitPoint)); if (result == null || result.VisualHit == null) { // Hit test failed. return false; } // // Actually want a reference to a 'ConnectorItem'. // The hit test may have hit a UI element that is below 'ConnectorItem' so // search up the tree. // var hitItem = result.VisualHit as FrameworkElement; if (hitItem == null) { return false; } var connectorItem = WpfUtils.FindVisualParentWithType<ConnectorItem>(hitItem); if (connectorItem == null) { return false; } var networkView = connectorItem.ParentNetworkView; if (networkView != this) { // // Ensure that dragging over a connector in another NetworkView doesn't // return a positive result. // return false; } object connectorDataContext = connectorItem; if (connectorItem.DataContext != null) { // // If there is a data-context then grab it. // When we are using a view-model then it is the view-model // object we are interested in. // connectorDataContext = connectorItem.DataContext; } connectorItemDraggedOver = connectorItem; connectorDataContextDraggedOver = connectorDataContext; return true; }
/// <summary> /// Event raised when the user starts to drag a connector. /// </summary> private void ConnectorItem_DragStarted(object source, ConnectorItemDragStartedEventArgs e) { this.Focus(); e.Handled = true; this.IsDragging = true; this.IsNotDragging = false; this.IsDraggingConnection = true; this.IsNotDraggingConnection = false; this.draggedOutConnectorItem = (ConnectorItem)e.OriginalSource; var nodeItem = this.draggedOutConnectorItem.ParentNodeItem; this.draggedOutNodeDataContext = nodeItem.DataContext != null ? nodeItem.DataContext : nodeItem; this.draggedOutConnectorDataContext = this.draggedOutConnectorItem.DataContext != null ? this.draggedOutConnectorItem.DataContext : this.draggedOutConnectorItem; // // Raise an event so that application code can create a connection and // add it to the view-model. // ConnectionDragStartedEventArgs eventArgs = new ConnectionDragStartedEventArgs(ConnectionDragStartedEvent, this, this.draggedOutNodeDataContext, this.draggedOutConnectorDataContext); RaiseEvent(eventArgs); // // Retrieve the the view-model object for the connection was created by application code. // this.draggingConnectionDataContext = eventArgs.Connection; if (draggingConnectionDataContext == null) { // // Application code didn't create any connection. // e.Cancel = true; return; } }