views:

111

answers:

1

I'm trying to drag and drop (slide) a Silverlight element from one part of a window to another.

I've implemented the MouseLeftButtonDown, MouseMove, and MouseLeftButtonUp event handlers on the element, but I've run into a bit of a problem.

All of the examples I've seen involve moving the element by setting the Canvas.Left and Canvas.Top properties. None of the elements I'm trying to manipulate live inside a Canvas. Is there a way to set the absolute position of the element being dragged, based on the coordinates of the mouse? Or is there a prepackaged solution to this problems somewhere that I've missed?

+3  A: 

All panels but Canvas use some kind of constraint to position their children. Only Canvas lets you use absolute positioning. That's why I think it is the only way to implement drag and drop .

Feel free to use a Canvas on top of your existing panel. Just remember to remove the dragged element from its original parent and put it in the Canvas (or drag some kind of copy) and do the reverse on mouse up.

Timores
I managed to get this working by dynamically creating a Canvas when a MouseLeftButtonDown event fired, using that Canvas to drag stuff around, and deleting the Canvas when MouseLeftButtonUp was fired. Kind of hackish, but it works.
JustinT