views:

335

answers:

2

Hi,

I added a Rectangle to a Canvas like this:

Canvas.SetTop(myRectangle, 150);
Canvas.SetLeft(myRectangle, 80);
canvas.Children.Add(myRectangle);

Now I want to move the rectangle to other place, say (100, 100). What is the best way to do this ?

Thanks !

+1  A: 

If you just want it there instantly, you'd just call SetTop and SetLeft again. Otherwise you'd use a StoryBoard, probably with 2 DoubleAnimationUsingKeyFrames elements that specify the Top and Left properties should change from 150/80 to 100/100 over your desired period of time (1 key frame for the initial value, 1 key frame for the final value).

David
I'm afraid this is not what I need.I'll explain what I'm trying to do.I want user to be able to draw circles on a Canvas.First, the user clicks somewhere on the Canvas to define circle's center. Then, when the mouse is moved, I would like to draw a circle according to the current mouse position (the radius is the distance between the current mouse position and the circle's center).I thought to implement this functionality by adding a new Ellipse to Canvas.Children, and then to move this Ellipse and change it's radius with each mouse movement. Am I on the right way ?
You'll want to edit your question, as the original definitely does not indicate you are attempting to do mouse based painting or anything like it at all. What you want to do then is use the MouseLeftButtonDown, MouseLeftButtonUp and MouseMove events. Down creates the shape and places it at the mouse position given in the event arguments. Move changes the shapes dimensions, and Up releases the shape.
David
A: 

I think a better way would be to create a rectangle centered at (0,0), and then use translate transforms to move it to where you want.

FryGuy
I'm not sure I understand the idea.Could you share some code sample ?Thanks !