/// <summary> /// Create relationship if possible. Release mouse afterwards. /// </summary> /// <param name="e"></param> protected override void OnMouseUp(MouseButtonEventArgs e) { if (hitItem != null) { object targetElement = hitItem.SelectedData; if (sourceItem != null && hitItem != null && this.diagramDesigner.CreateRelationshipCommand != null) { ViewModelRelationshipCreationInfo info = new ViewModelRelationshipCreationInfo(sourceItem.SelectedData, hitItem.SelectedData); // calcualte source and target points if (hitItem is FrameworkElement) { FrameworkElement sourceFElement = sourceConnector.DiagramItem; double sourceLeft = Canvas.GetLeft(sourceFElement); double sourceTop = Canvas.GetTop(sourceFElement); FrameworkElement targetFElement = hitItem as FrameworkElement; double targetLeft = Canvas.GetLeft(targetFElement); double targetTop = Canvas.GetTop(targetFElement); if (!double.IsNaN(sourceLeft) && !double.IsNaN(sourceTop) && !double.IsNaN(targetLeft) && !double.IsNaN(targetTop)) { Point proposedTarget = e.GetPosition(this.diagramDesigner); PointD proposedSourcePoint = new PointD(startPoint.X, startPoint.Y); PointD proposedTargetPoint = new PointD(proposedTarget.X, proposedTarget.Y); // calculate points //Point sourcePointT = sourceFElement.TranslatePoint(new Point(0, 0), this.diagramDesigner); //Point targetPointT = targetFElement.TranslatePoint(new Point(0, 0), this.diagramDesigner); Point sourcePoint = sourceFElement.TransformToAncestor(this.diagramDesigner).Transform(new Point(0, 0)); Point targetPoint = targetFElement.TransformToAncestor(this.diagramDesigner).Transform(new Point(0, 0)); // calculate side for source RectangleD sourceBounds = new RectangleD(sourcePoint.X, sourcePoint.Y, sourceFElement.Width, sourceFElement.Height); info.ProposedSourcePoint = LinkShape.CalculateLocation(sourceBounds, proposedSourcePoint); // calculate side for target RectangleD targeteBounds = new RectangleD(targetPoint.X, targetPoint.Y, targetFElement.Width, targetFElement.Height); info.ProposedTargetPoint = LinkShape.CalculateLocation(targeteBounds, proposedTargetPoint); } } this.diagramDesigner.CreateRelationshipCommand.Execute(info); } } if (this.IsMouseCaptured) this.ReleaseMouseCapture(); AdornerLayer adornerLayer = AdornerLayer.GetAdornerLayer(this.diagramDesigner); if (adornerLayer != null) { adornerLayer.Remove(this); } }
/// <summary> /// Hit testing. /// </summary> /// <param name="hitPoint">Location to do a hit test at.</param> private void HitTesting(Point hitPoint) { DependencyObject hitObject = diagramDesigner.InputHitTest(hitPoint) as DependencyObject; while (hitObject != null ) { if (hitObject is DiagramDesignerItem) { hitItem = hitObject as DiagramDesignerItem; if (!hitItem.IsDiagramLink) { if (sourceItem != null && hitItem != null && this.diagramDesigner.CreateRelationshipCommand != null) { object sourceData = sourceItem.SelectedData; object targetData = hitItem.SelectedData; if (sourceData != null && targetData != null) { ViewModelRelationshipCreationInfo info = new ViewModelRelationshipCreationInfo(sourceData, targetData); if (info.Source != null && info.Target != null) if (this.diagramDesigner.CreateRelationshipCommand.CanExecute(info)) { return; } } } } hitItem = null; return; } hitObject = VisualTreeHelper.GetParent(hitObject); } hitItem = null; }