private void Rec_MouseLeftButtonDown(object sender, MouseButtonEventArgs e) { NewCanvas temp = sender as NewCanvas; // Clone selected item DraggedItem = temp.Clone(); // Add event handler DraggedItem.MouseLeftButtonUp += new MouseButtonEventHandler(Rec_MouseLeftButtonUp); DraggedItem.MouseMove += new System.Windows.Input.MouseEventHandler(Rec_MouseMove); // Add to parent element //Console.WriteLine(DraggedItem.Parent); // Move clone to RootCanvas // Group_Basic.Children.Add(DraggedItem); var canvas = temp.Parent as Canvas; //var grid = canvas.Parent as Border; RecPos.X = (Double)DraggedItem.GetValue(Canvas.LeftProperty) + (Double)canvas.Parent.GetValue(Canvas.LeftProperty) + (Double)Canvas1.GetValue(Canvas.LeftProperty); RecPos.Y = (Double)DraggedItem.GetValue(Canvas.TopProperty) + (Double)canvas.Parent.GetValue(Canvas.TopProperty) + (Double)Canvas1.GetValue(Canvas.TopProperty) + Define.YPosOffset_OnDragging; DraggedItem.SetValue(Canvas.TopProperty, RecPos.Y); DraggedItem.SetValue(Canvas.LeftProperty, RecPos.X); //Console.WriteLine("(x, y) = (" + RecPos.X + ", " + RecPos.Y + ")"); RootCanvas.Children.Add(DraggedItem); // drag setup MousePos = e.GetPosition(null); PosDelta.X = RecPos.X - MousePos.X; PosDelta.Y = RecPos.Y - MousePos.Y; IsDragging = true; // Update log Console.WriteLine(DateTime.Now.ToString() + " : Start dragging <" + DraggedItem.Name + ">"); }
// Generate deep copies public NewCanvas Clone() { NewCanvas temp = new NewCanvas(); ushort cnt = Define.Counter[this.Index]++; temp.Index = this.Index; //temp.AnchorPointType = this.AnchorPointType; temp.Name = this.Name + cnt; temp.Height = this.Height; temp.Width = this.Width; temp.Cursor = this.Cursor; temp.ThemeColor = this.ThemeColor; temp.Code_Prefix = this.Code_Prefix; temp.Code_Suffix = this.Code_Suffix; temp.Device_Addr = this.Device_Addr; //temp.Background = this.Background; temp.SetValue(Canvas.LeftProperty, this.GetValue(Canvas.LeftProperty)); temp.SetValue(Canvas.TopProperty, this.GetValue(Canvas.TopProperty)); temp.SetValue(Canvas.ZIndexProperty, Define.ZIndex_OnDragging); for (int i = 0; i < this.Children.Count; i++) { if (this.Children[i] is NewLabel newLabel) { temp.Children.Add(newLabel.Clone()); } else if (this.Children[i] is NewTextBox newTextBox) { temp.Children.Add(newTextBox.Clone()); } else if (this.Children[i] is NewTextBlock newTextBlock) { temp.Children.Add(newTextBlock.Clone()); } else if (this.Children[i] is NewBorder newBorder) { temp.Children.Add(newBorder.Clone()); } // =========================================== // Add new elements here // =========================================== } // Write log to console Console.WriteLine(DateTime.Now.ToString() + " : Clone <" + this.Name + "> to <" + temp.Name + ">"); return(temp); }
private void Rec_MouseLeftButtonUp(object sender, MouseButtonEventArgs e) { if (!IsDragging) { return; } // Get the position on Canvas2 DraggedItem.SetValue(Canvas.LeftProperty, (Double)DraggedItem.GetValue(Canvas.LeftProperty) - (Double)Canvas2.GetValue(Canvas.LeftProperty)); DraggedItem.SetValue(Canvas.TopProperty, (Double)DraggedItem.GetValue(Canvas.TopProperty) - (Double)Canvas2.GetValue(Canvas.TopProperty)); // Item is on Canvas2 or not if ((Double)DraggedItem.GetValue(Canvas.LeftProperty) > 0 && (Double)DraggedItem.GetValue(Canvas.TopProperty) > 0) { IsInArea = true; } else { IsInArea = false; } // Position is legal or not if (IsInArea && (IsEmpty || IsAnchored)) { // Add to Canvas2 if (IsEmpty) { IsEmpty = false; DraggedItem.AnchorPointType = Anchors.AnchorType_FirstBlock; } else { DraggedItem.AnchorPointType = AnchorIndex; } RootCanvas.Children.Remove(DraggedItem); Canvas2.Children.Add(DraggedItem); Console.WriteLine(DateTime.Now.ToString() + " : Move <" + DraggedItem.Name + "> to <Canvas2>"); NewCanvasList.Add(DraggedItem); Console.WriteLine(DateTime.Now.ToString() + " : Add <" + DraggedItem.Name + "> to List"); // If is not anchored at the right anchor point if (AnchorIndex != (Define.AnchorNum - 1)) { // Update anchor point Anchor[1].X = (Double)DraggedItem.GetValue(Canvas.LeftProperty) + (Double)Canvas2.GetValue(Canvas.LeftProperty); Anchor[1].Y = (Double)DraggedItem.GetValue(Canvas.TopProperty) + (Double)Canvas2.GetValue(Canvas.TopProperty) + DraggedItem.Height + Anchors.VerticalMargin; Anchor[0].X = Anchor[1].X - Anchors.Indent; Anchor[0].Y = Anchor[1].Y; Anchor[2].X = Anchor[1].X + Anchors.Indent; Anchor[2].Y = Anchor[1].Y; Anchor[3].X = (Double)DraggedItem.GetValue(Canvas.LeftProperty) + (Double)Canvas2.GetValue(Canvas.LeftProperty) + DraggedItem.Width + Anchors.HorizontalMargin; Anchor[3].Y = (Double)DraggedItem.GetValue(Canvas.TopProperty) + (Double)Canvas2.GetValue(Canvas.TopProperty); Console.WriteLine(DateTime.Now.ToString() + " : Update anchor points"); } } else { // Delete illegal item Console.WriteLine(DateTime.Now.ToString() + " : Illegal placement. Delete <" + DraggedItem.Name + ">"); RootCanvas.Children.Remove(DraggedItem); DraggedItem = null; } IsDragging = false; // Update log Console.WriteLine(DateTime.Now.ToString() + " : Finish dragging"); }