You can use the Microsoft Surface drag-and-drop framework to include multiple, simultaneous, drag-and-drop operations in your Surface applications. The framework includes a class that represents an item being dragged, and classes that define the events that occur during a drag-and-drop operation. Some Surface controls are enabled for drag-and-drop by default, and other controls can be enabled by setting a property and hooking up the events.
How the Surface Drag-and-Drop Framework Works
During a drag-and-drop operation, there are three concepts involved:
- The source This is the container the dragged item comes from.
- The cursor This is the visual representation of the data (or item) being dragged.
- The target This is the container into which the dragged item will be dropped.
Classes Involved in Drag-and-Drop Operations
The following classes are involved in drag-and-drop operations.
Represents the element (and associated data) that is being dragged. This class includes a visual representation of the drag cursor that you define.
This object is returned when you start a drag-and-drop operation by calling BeginDragDrop, and serves as a handle to the drag-and-drop APIs. Surface applications can support multiple, simultaneous drag-and-drop operations because of this object.
Provides static properties and methods to perform drag-and-drop operations. This class defines the attached events that you can use to listen for drag-and-drop operations, and it includes methods to do the following:
Represents event arguments for an attached event that the QueryTargetEvent field identifies. This event is raised on each element that can accept a dropped item.
Represents event arguments for an attached event that the TargetChangedEvent field identifies. This event is raised as the potential drop target changes.
Represents event arguments for an attached event that the GiveFeedbackEvent field identifies. This event is raised when a drag operation starts and during the drag movement.
Represents event arguments that are used for drag-and-drop events.
Represents event arguments for an attached event that the DragCompletedEvent field identifies. This event is raised on the drag source object when the drag operation is complete.
Surface Controls that are Enabled for Drag-and-Drop by Default
The following Surface controls include built-in drag-and-drop functionality:
For more information about these controls, see LibraryBar, LibraryContainer, and LibraryStack Controls.
Enabling Drag-and-Drop on Other Controls
For other controls, you can use the Surface drag-and-drop framework to implement drag-and-drop functionality in your application.
The following topics describe how to use the drag-and-drop framework to enable common drag-and-drop scenarios:
- Dragging and Dropping Items Between SurfaceListBox Controls
- Dragging and Dropping Items from ScatterView Controls to SurfaceListBox Controls
- Dragging and Dropping Items from ScatterView Controls to SurfaceListBox Controls (Alternative Approach)
- Custom Hit Testing During a Drag-and-Drop Operation
- Creating Visual Cues During a Drag-and-Drop Operation
Complete Code Sample
The Shopping Cart SDK sample shows how to use the drag-and-drop framework to move items from a SurfaceListBox to a ScatterView, and from the ScatterView to a LibraryStack that serves as a shopping cart.
When a user drags an item, the code in the StartDragDrop() method (called from PreviewMouseMove and PreviewTouchMove event handlers) determines whether to start a drag-and-drop operation by checking the following criteria and taking their respective actions:
Is the input device in the list of input devices to ignore? If so, exit the method.
Has the user dragged the item past a specific threshold horizontally? If so, he or she must be scrolling the items in the SurfaceListBox and he or she must not want to drag the item. In this case, add the input device to the list of input devices to ignore.
Has the user dragged the item past a specific threshold vertically? If not, don't do anything yet. If so, and the input device is touching one of the items in the SurfaceListBox, try to start the drag-and-drop operation.
© Microsoft Corporation. All rights reserved.