3
votes

I am creating shapes on a wpf using a Canvas. I have created by C# code some Rectangles, each one of which is assigned with three circles and a TextBlock. I want now to make them move on the Canvas by a mouseEvent , in other words to drag them with the mouse and to move them on the Canvas. How is it possible to manipulate each Rectangle with the circles and the TextBlock as a compact group?

It is required, when the user clicks on the Rectangle to transfer it with its contents. However, the circles and the textBlock have their own coordinates, so if not grouped they stand still. How can I get over this?

2
You can do this by simply calculating bounding-box for compact group, create a new invisible or outlined canvas in main canvas and transfer compact group items to new canvas and adjust coordinates, then move the new canvas with mouse, when over, revert back group items to main canvas from new canvas, also adjust coordinates. May this helps !Code0987

2 Answers

2
votes

Put the rectangle and everything else in a grid or another canvas, as described in this related question of yours: Drawing circles on a Rectangle

This is basically a user control composed of the rectangles, circles, and textbox, and you just allow that whole user control to be moved around, rather than the constituent parts.

1
votes

If you don't want to add another layout panels try to apply the same transform to all elements you want to move, it's not so heavy solution.