private void rect_MouseMove(object sender, MouseEventArgs e) { var rect = (Rectangle)sender; if (!rect.IsMouseCaptured) { return; } // get the position of the mouse relative to the Canvas var mousePos = e.GetPosition(RoomCanvas); // center the rect on the mouse double left = mousePos.X - (rect.ActualWidth / 2); double top = mousePos.Y - (rect.ActualHeight / 2); if (isEdit) {//boundary for left side if (left < 0) { left = 0; } //boundary for right side if (left > this.ActualWidth - rect.ActualWidth * 1.16) { left = this.ActualWidth - rect.ActualWidth * 1.16; } //boundary for top if (top < 0) { top = 0; } //boundary for bottom if (top > this.ActualHeight - rect.ActualHeight * 2.58) { top = this.ActualHeight - rect.ActualHeight * 2.58; } Canvas.SetLeft(rect, left); Canvas.SetTop(rect, top); room.SetLeft(Canvas.GetLeft(rect)); room.SetTop(Canvas.GetTop(rect)); } }
private void SaveClickEventHandler(object sender, RoutedEventArgs e) => CreateRoom(); //call cancel method public void CreateRoom() { if (isEdit) { room = new RoomData(); //cast object var rect = (Rectangle)room.makeRoom(); //tract mouse events rect.MouseLeftButtonDown += rect_MouseLeftButtonDown; rect.MouseLeftButtonUp += rect_MouseLeftButtonUp; rect.MouseMove += rect_MouseMove; //add room object to room list //add object to canvas RoomCanvas.Children.Add(rect); room.SetLeft(Canvas.GetLeft(rect)); room.SetTop(Canvas.GetTop(rect)); Console.WriteLine(Canvas.GetLeft(rect)); Console.WriteLine(Canvas.GetTop(rect)); } }