private ConnectionDiagramBase GetSenderDiagram(object sender) { ConnectionDiagramBase element = null; if (!(sender is ConnectionDiagramBase)) { element = WPFUtility.FindVisualParent <ConnectionDiagramBase>(sender as UIElement); } else { element = sender as ConnectionDiagramBase; } return(element); }
internal static DiagramLayout CreateDiagramLayout(ConnectionDiagramBase diagram, int index = 0) { if (diagram == null) { return(null); } return(new DiagramLayout() { TypeName = diagram.GetType().FullName, Top = (double)diagram.GetValue(Canvas.TopProperty), Left = (double)diagram.GetValue(Canvas.LeftProperty), PositionType = diagram.ConnectorPositionType, DataContext = diagram.DataContext, Index = index, }); }
internal static void SetConnectionLinePosition(ConnectionDiagramBase element, Point point) { if (element == null) { return; } if (element.DepartureLines != null && element.DepartureLines.Any()) { foreach (var dline in element.DepartureLines) { if (dline is ILinePosition) { (dline as ILinePosition).X1 = (dline as ILinePosition).X1 + point.X; (dline as ILinePosition).Y1 = (dline as ILinePosition).Y1 + point.Y; var angle = Math.Atan2( (dline as ILinePosition).Y1 - (dline as ILinePosition).Y2, (dline as ILinePosition).X1 - (dline as ILinePosition).X2) * 180d / Math.PI; (dline as ILinePosition).Angle = angle < 0 ? angle + 360 : angle; } } } if (element.ArrivalLines != null && element.ArrivalLines.Any()) { foreach (var aline in element.ArrivalLines) { if (aline is ILinePosition) { (aline as ILinePosition).X2 = (aline as ILinePosition).X2 + point.X; (aline as ILinePosition).Y2 = (aline as ILinePosition).Y2 + point.Y; var angle = Math.Atan2( (aline as ILinePosition).Y1 - (aline as ILinePosition).Y2, (aline as ILinePosition).X1 - (aline as ILinePosition).X2) * 180d / Math.PI; (aline as ILinePosition).Angle = angle < 0 ? angle + 360 : angle; } } } }
private void AssociatedObject_Drop(object sender, DragEventArgs e) { if (_dataType != null) { //if the data type can be dropped if (e.Data.GetDataPresent(_dataType)) { FrameworkElement element = sender as FrameworkElement; ICommand dropcommand = GetDropLineCommand(element); if (dropcommand != null) { object parameter = GetDropLineCommandParameter(element); Point point = e.GetPosition(element); var adn = e.Data.GetData(_dataType) as DrawLineAdorner; Point adnPoint = adn.Position; Canvas droppedcanvas = GetDroppedLineCanvas(element); var originelement = (e.Data.GetData(typeof(UIElement)) as FrameworkElement); //the object for ConnectionDiagram ConnectionDiagramBase origindiagram = originelement as ConnectionDiagramBase; ConnectionDiagramBase terminaldiagram = element as ConnectionDiagramBase; SetDraggedDataContext(this.AssociatedObject, e.Data.GetData(DataFormats.Serializable)); if (droppedcanvas != null) { double x1, y1, x2, y2 = 0.0; if (ConnectorPositionType.Custom.Equals(origindiagram?.ConnectorPositionType)) { //the Ghost line position x1 = adn.GetLineStartEndPosition().Item1; y1 = adn.GetLineStartEndPosition().Item2; } else { x1 = WPFUtility.GetCenterPosition(originelement, droppedcanvas).X; y1 = WPFUtility.GetCenterPosition(originelement, droppedcanvas).Y; } if (ConnectorPositionType.Custom.Equals(terminaldiagram?.ConnectorPositionType)) { //the Ghost line position x2 = adn.GetLineStartEndPosition().Item3; y2 = adn.GetLineStartEndPosition().Item4; } else { x2 = WPFUtility.GetCenterPosition(element, droppedcanvas).X; y2 = WPFUtility.GetCenterPosition(element, droppedcanvas).Y; } //the line type of custom Type linetype = GetDropLineControlType(element); if (!WPFUtility.IsCorrectType(linetype, typeof(ConnectionLineBase))) { throw new ArgumentException($"DropLineControlType is base on {nameof(ConnectionLineBase)}."); } dynamic conline; if (!typeof(DrawLineThump).Equals(linetype)) { conline = Activator.CreateInstance(linetype); if (conline is ConnectionLineBase) { //if inherit from ILinePosition if (conline is ILinePosition) { (conline as ILinePosition).X1 = x1; (conline as ILinePosition).Y1 = y1; (conline as ILinePosition).X2 = x2; (conline as ILinePosition).Y2 = y2; } } } else { conline = new Line() { X1 = x1, Y1 = y1, X2 = x2, Y2 = y2, }; } FrameworkElementAssist.SetOriginDiagram(conline, originelement); FrameworkElementAssist.SetTerminalDiagram(conline, element); //set the parameter //var dropparam = parameter ?? conline.DataContext; string lineuuid = $"{conline.GetType().Name}_{Guid.NewGuid().ToString()}"; if (dropcommand.CanExecute(parameter)) { Canvas.SetTop(conline, 0); Canvas.SetLeft(conline, 0); //add the relation of the diagram if (origindiagram != null) { origindiagram.DepartureLines.Add(conline); } if (terminaldiagram != null) { terminaldiagram.ArrivalLines.Add(conline); } //TODO:FrameworkElement???? if (droppedcanvas != null) { droppedcanvas.Children.Add(conline); } //Set the Dropping Line DataContext SetDroppingDataContext(this.AssociatedObject, conline.DataContext); dropcommand.Execute(parameter); if (conline is ConnectionLineBase) { (conline as ConnectionLineBase).LineUUID = lineuuid; } else if (conline is Line) { FrameworkElementAssist.SetLineUUID(conline, lineuuid); } } } else { dropcommand.Execute(parameter); } } } } if (this._adorner != null) { this._adorner.Remove(); } e.Handled = true; return; }