Пример #1
0
        /// <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;
            }
        }
Пример #2
0
        /// <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 == _draggedOutConnectorItem);

            var 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, _draggedOutNodeDataContext, _draggingConnectionDataContext, _draggedOutConnectorDataContext, connectorDataContextDraggedOver));

            IsDragging                      = false;
            IsNotDragging                   = true;
            IsDraggingConnection            = false;
            IsNotDraggingConnection         = true;
            _draggedOutConnectorDataContext = null;
            _draggedOutNodeDataContext      = null;
            _draggedOutConnectorItem        = null;
            _draggingConnectionDataContext  = null;
        }
Пример #3
0
        /// <summary>
        /// Event raised while the user is dragging a connector.
        /// </summary>
        private void ConnectorItem_Dragging(object source, ConnectorItemDraggingEventArgs e)
        {
            e.Handled = true;

            Trace.Assert((ConnectorItem)e.OriginalSource == _draggedOutConnectorItem);

            var mousePoint = Mouse.GetPosition(this);
            //
            // Raise an event so that application code can compute intermediate connection points.
            //
            var connectionDraggingEventArgs =
                new ConnectionDraggingEventArgs(ConnectionDraggingEvent, this,
                                                _draggedOutNodeDataContext, _draggingConnectionDataContext,
                                                _draggedOutConnectorDataContext);

            RaiseEvent(connectionDraggingEventArgs);

            //
            // Figure out if the connection has been dragged over a connector.
            //

            ConnectorItem connectorDraggedOver            = null;
            object        connectorDataContextDraggedOver = null;
            var           dragOverSuccess = DetermineConnectorItemDraggedOver(mousePoint, out connectorDraggedOver, out connectorDataContextDraggedOver);

            if (connectorDraggedOver != null)
            {
                //
                // Raise an event so that application code can specify if the connector
                // that was dragged over is valid or not.
                //
                var queryFeedbackEventArgs =
                    new QueryConnectionFeedbackEventArgs(QueryConnectionFeedbackEvent, this, _draggedOutNodeDataContext, _draggingConnectionDataContext,
                                                         _draggedOutConnectorDataContext, connectorDataContextDraggedOver);

                RaiseEvent(queryFeedbackEventArgs);

                if (queryFeedbackEventArgs.FeedbackIndicator != null)
                {
                    //
                    // A feedback indicator was specified by the event handler.
                    // This is used to indicate whether the connection is good or bad!
                    //
                    AddFeedbackAdorner(connectorDraggedOver, queryFeedbackEventArgs.FeedbackIndicator);
                }
                else
                {
                    //
                    // No feedback indicator specified by the event handler.
                    // Clear any existing feedback indicator.
                    //
                    ClearFeedbackAdorner();
                }
            }
            else
            {
                //
                // Didn't drag over any valid connector.
                // Clear any existing feedback indicator.
                //
                ClearFeedbackAdorner();
            }
        }
Пример #4
0
        /// <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);
        }