protected override void OnMouseMove(MouseEventArgs e) { base.OnMouseMove(e); if (e.RightButton == MouseButtonState.Pressed) { Point _actualMousePosition = Mouse.GetPosition(this); if (Point.Subtract(_copyOperationMousePosition, _actualMousePosition).Length > 10) { //copying should occur when dragged DesignerCanvas designer = VisualTreeHelper.GetParent(this) as DesignerCanvas; var selectedItems = from item in designer.SelectionService.CurrentSelection.OfType <DesignerItem>() select item; List <ElementVM> selectedElementsVM = new List <ElementVM>(); foreach (DesignerItem item in selectedItems) { if (item.DataContext is ElementVM elementVM) { selectedElementsVM.Add(elementVM); } } var selectedConnections = from connection in designer.SelectionService.CurrentSelection.OfType <Connection>() select connection; List <ConnectionVM> selectedConnectionsVM = new List <ConnectionVM>(); foreach (Connection connection in selectedConnections) { if (connection.DataContext is ConnectionVM connectionVM) { selectedConnectionsVM.Add(connectionVM); } } string returnedString = (designer.DataContext as MainVM).WriteElementsAndConnectionsToString(selectedElementsVM, selectedConnectionsVM); DragString dragString = new DragString(); dragString.DraggedString = returnedString; dragString.DragPointStart = Mouse.GetPosition(designer); DragDrop.DoDragDrop(this, dragString, DragDropEffects.Copy); } } }
protected override void OnDrop(DragEventArgs e) { //base.OnDrop(e); #region FileImport if (e.Data.GetDataPresent(DataFormats.FileDrop)) { // Can have more than one file. string[] files = (string[])e.Data.GetData(DataFormats.FileDrop); List <string> validSNPFiles = new List <string>(); foreach (string file in files) { if (TouchstoneFile.ExtensionIsValid(file)) { validSNPFiles.Add(file); } } if (validSNPFiles.Count > 0) { Point position = e.GetPosition(this); Vector positionOffset = new Vector(0, 0); foreach (string validFile in validSNPFiles) { TouchstoneFile touchstoneFile = new TouchstoneFile(); TouchstoneResult touchstoneResult; try { touchstoneResult = touchstoneFile.Read(validFile); } catch (Exception exception) { Console.WriteLine(exception); throw; } if (touchstoneResult != null) { ImportedComponentModel importedComponentModel = new ImportedComponentModel(); importedComponentModel.Size = new Size(60, 50); importedComponentModel.Position = Point.Add(position, positionOffset); positionOffset = Vector.Add(positionOffset, new Vector(10, 10)); importedComponentModel.LoadFromTouchstoneResult(touchstoneResult); importedComponentModel.FileName = validFile; importedComponentModel.Name = Path.GetFileNameWithoutExtension(validFile); if (DataContext is MainVM mainVM) { if (mainVM.AddElement is RelayCommand relayCommand) { relayCommand.Run(importedComponentModel); } else { throw new Exception("MainVM AddElement Command should be type of RelayCommand"); } } else { throw new Exception("DataContext of DesignerCanvas should be MainVM type"); } } } } } #endregion #region Drag from toolbox / Copy by drag DragObject dragObject = e.Data.GetData(typeof(DragObject)) as DragObject; DragString dragString = e.Data.GetData(typeof(DragString)) as DragString; if (dragObject != null) { Point position = e.GetPosition(this); double width = dragObject.DesiredSize.Width; double height = dragObject.DesiredSize.Height; double left = Math.Max(0, position.X - width / 2); double top = Math.Max(0, position.Y - height / 2); Element NewElement = Creator.CreateElementByType(dragObject.Type, width, height, top, left); if (DataContext is MainVM mainVM) { if (mainVM.AddElement is RelayCommand relayCommand) { relayCommand.Run(NewElement); } else { throw new Exception("MainVM AddElement Command should be type of RelayCommand"); } } else { throw new Exception("DataContext of DesignerCanvas should be MainVM type"); } e.Handled = true; } else if (dragString != null) { if (DataContext is MainVM mainVM) { Vector elementsShift = Point.Subtract(e.GetPosition(this), dragString.DragPointStart); mainVM.ReadAndShiftElementsAndConnectionsFromString(dragString.DraggedString, elementsShift); } } #endregion }