locked
Drag and drop in a GridView

    Question

  • Hi,

    I'm trying to implement a drag and drop operation on a GridView containing four rectangles (red, green, blue, yellow) laid out 2x2. What I'd like to do is start a drag operation over any rectangle within the GridView and drop over a destination rectangle in the same GridView, the result of which would be to swap the locations of those two rectangles in the grid view.

    I'm assuming I can do this by identifying the rectangles under the drag start and the drag drop, and swapping their locations in the underlying rectangle collection.

    I've enabled CanDragItems and AllowDrop, but not CanReorderItems since that seemed to conflict.

    I think I've got the drag start operation OK. By handling the DragItemsStarting event, I can identify the source object from the DragStartingEventsArgs::Items property. I'm also handling the Drop event, but my problem is that I don't know how to identify the item I'm dropping over. The drop event provides a DragEventArgs, but I'm not sure how get the data I need out of that.

    A quick internet search showed that in WPF listboxes have a method called IndexFromPoint(), which I think gives the listbox index that the mouse is over. Is this the sort of thing I need to use?

     
    Tuesday, December 11, 2012 9:30 PM

Answers

  • Hi,

    I think I found my answer. The item being dropped over is DragEventArgs::OriginalSource.

    • Marked as answer by Jesse Jiang Thursday, December 13, 2012 6:44 AM
    Wednesday, December 12, 2012 12:13 AM

All replies