tags:

views:

373

answers:

2

I have a wpf application that has some shapes on a canvas I want to allow the user to click on a shape and then the shape gets stuck the the mouse until they click again.

So far I know very little about WPF so go easy on me ;)

A: 

Hopefully this is what you're looking for.

"Mouse dragging logic is fairly straightforward: In the OnMouseDown handler, you save the position of both the object you want to drag and the mouse pointer, and you call CaptureMouse. In OnMouseMove, you calculate the difference between the coordinates of the current mouse pointer position and the saved position, and add that to the original object position. (If you're on a Canvas, you can move the object by calling Canvas.SetLeft and Canvas.SetTop for the object; otherwise you can adjust a TranslateTransform object set to the object's RenderTransform property.) In OnMouseUp, you call ReleaseCapture.

Because your app can lose the mouse capture in other ways (such as the appearance of a system modal message box), you'll also want to override OnLostMouseCapture to abort the dragging operation (if it hasn't terminated with OnMouseUp) and perform cleanup. You might also want to override OnTextInput to abort the drag if the user presses the Escape key."

Copied from http://social.msdn.microsoft.com/Forums/en-US/wpf/thread/b6c51eef-269e-4c85-96af-b5b1e4cb9bd5/ there's also code up on this site for how to do it.

A: 

Check out this Thread - http://silverlight.net/forums/t/68889.aspx

Since your 'Stick' is on the Canvas, keep setting the Canvas.Left and Canvas.Top on MouseMove with MousePositions

Jobi Joy