Drag and Drop in WPF

Drag-and-drop operations typically involve two parties: a drag source from which the dragged object originates and a drop target which receives the dropped object. The drag source and drop target may be the same application or a different application.

The type and number of objects that can be manipulated with drag-and-drop is completely arbitrary; one or more files or folders, and selections of content are some of the more common objects manipulated through drag-and-drop operations. The particular action(s) performed during a drag-and-drop operation are application specific, and often determined by context. For example, dragging a selection of files from one folder to another on the same storage device moves the files by default, whereas dragging files from a Universal Naming Convention (UNC) share to a local folder copies the files by default. The drag-and-drop facilities provided by the WPF are designed to be highly flexible and customizable to support a wide variety of drag-and-drop scenarios. Drag-and-drop supports manipulating objects within a single application, or between different applications; dragging-and-dropping between WPF applications and other Windows applications is also fully supported. Drag-and-drop falls under the more general area of data transfer; in addition to drag-and-drop, data transfer also includes using the system clipboard for copy and paste operations.

Drag-and-drop操作涉及到三方:drag source, dragged object, and drag target. 也就是把一个对象从某处拖动到另外一处。可以拖动的对象是比较广泛的,包括文件、文件夹,选中的部分文字。

被拖动的数据存储在一个数据对象中,一个数据对象包括一个Object(包含了真正的数据),一个相应的数据格式描述符(描述数据是什么格式,或者数据可以被转换成什么格式)。所有的数据对象都必须实现IDataObject接口。IDataObject接口定义的方法包括GetData,GetDataPresent,GetFormats和SetData。

Drag-and-drop操作过程中,会由一系列事件,DragSource包含的Event包括,GiveFeedback、QueryContinueDrag、PreviewGiveFeedback 和 PreviewQueryContinueDrag;DragTarget包含的Event包括:DragEnter[dragged object 进入这个target的边界时],DragLeave[dragged object 离开garget的边界之外],DragOver[dragged object 在target范围内移动],Drop[dragged object 被放置到target内],PreviewDragEnter,PreviewDragLeave,PreviewDragOver,PreviewDrop。

这些Event有两种模型,一个是Bubbling,一个是Tunnelling(带Preview前缀的)。

Bubbling的event是动作发生后触发;Tunnelling的event是动作发生前触发。

注意:DragTarget要触发这些事件,需要setting the AllowDrop attribute to true on the target element.

Use a Thumb to Enable Dragging

The Thumb control provides drag functionality that can be used to move or resize controls by monitoring the DragStarted, DragDelta and DragCompleted events of the Thumb.

The user begins a drag operation by pressing the left mouse button when the mouse pointer is paused on the Thumb control. The drag operation continues as long as the left mouse button remains pressed. During the drag operation, the DragDelta can occur more than once. Each time it occurs, the DragDeltaEventArgs class provides the change in position that corresponds to the change in mouse position. When the user releases the left mouse button, the drag operation is finished. The drag operation only provides new coordinates; it does not automatically reposition the Thumb.

 

你可能感兴趣的:(drag)