DragSource and DropTarget
DragSource and DropTarget in UIToolbox are manipulators for drag and drop between elements at runtime. The UI Toolkit drag events (DragEnterEvent and the others) work only in the Editor. The MDI drop system moves windows and tabs only. The Inventory example drags items between slots with them.


// Drag the item of a slot. Null data starts no drag.
slotElement.AddManipulator(
new DragSource(() => slot.Item != null ? slot : null, () =>
MakeTile(slot.Item)));
// Take a slot whose item fits here.
slotElement.AddManipulator(
new DropTarget(data => Move((Slot)data, slot), data =>
data is Slot from && CanMove(from, slot)));
| Member | Description |
|---|---|
new DragSource(data, makeGhost) |
data gives the dragged object at the drag start. makeGhost makes the element that follows the pointer. Without it, the ghost is an empty element with the size and classes of the source. |
DragSource.Threshold |
The pixels the pointer moves before a press becomes a drag. Default 6. A shorter move stays a click. |
DragSource.AutoScroll |
A drag near the edge of a ScrollView scrolls it: inside AUTO_SCROLL_EDGE pixels (40, or a quarter of the view when it is smaller), up to AUTO_SCROLL_SPEED pixels a second (800) at the edge. Default true. |
DragSource.Tilt |
The ghost gets tb-drag-ghost--tilt, which turns it 3 degrees. False keeps the ghost straight. Default true. |
DragSource.IsDragging, Ghost |
The drag state and the ghost element. |
DragSource.Ended |
Raised after the drop or the cancel, with the data and the target that took it, or null. |
DragSource.Denied |
Raised before Ended when the pointer goes up over a target that does not take the data, with the data and that target. |
new DropTarget(dropped, canDrop) |
dropped gets the data of a drop. canDrop decides which data the target takes; null takes all data. |
new DropTarget((data, index) => ..., canDrop) |
A list target. See List targets. |
DropTarget.CanDrop(data), Drop(data) |
The filter, and a drop from code. A keyboard or gamepad move can call Drop. A list target puts the data at the end. Returns false when the target does not take the data. |
DropTarget.Drop(data, index) |
A drop from code at a place of a list target. The index is clamped to the items. A target that is not a list ignores it. |
DropTarget.At(element) |
The target on the element or its nearest ancestor. |
- The ghost draws over every element of the document of the source, such as a HUD next to the workspace. It goes into the top element of the document, inside empty layers with the classes and style sheets of the elements down to the nearest
Workspace, so it keeps the workspace theme and the USS rules of the ancestors. The layers go at the end of the drag. The ghost ignores the pointer, and it centers on the pointer. Its margins are set to 0. - A drop over a child of a target goes to the target.
- A drag holds the pointer capture. A lost capture cancels the drag.
List targets
A list target gives the place of the drop as well as the data. Make it with a dropped callback that takes the data and an index. During a drag over the target, a line shows where the data goes in, between two items. The KanbanDemo example has a column of tasks in a ScrollView as a list target.
var list = new ScrollView(ScrollViewMode.Vertical);
list.AddManipulator(new DropTarget((data, index) =>
{
var task = (Task)data;
// Insert takes the task out first, so a task that moves down in this
// list goes one place less
if (task.Element.parent == list
&& list.IndexOf(task.Element) < index) index--;
list.Insert(index, task.Element);
}, data => data is Task));
// Move the selected task one place up without a drag
DropTarget.At(list).Drop(task, list.IndexOf(task.Element) - 1);
| Member | Description |
|---|---|
index |
The index among the children of the contentContainer of the target: before the first visible item whose middle is past the pointer. The items can be a column or a row, as the flex direction of the contentContainer sets. The index counts the dragged item when it is in the same list. |
DropTarget.IsList |
True for a list target. |
DropTarget.IndexAt(position) |
The index for a drop at a position in panel coordinates. |
DropTarget.InsertLine |
The line while the data is over the target, otherwise null. |
- The line shows only when the target takes the data.
- The line is 2 pixels thick, across the list. It goes into the nearest overlay host above the target, so a
ScrollViewdoes not clip it.
Styling
| Class | Element |
|---|---|
tb-drag-source--dragging |
The source during a drag: opacity 0.4. |
tb-drag-ghost |
The element that follows the pointer: opacity 0.85. |
tb-drag-ghost--tilt |
The ghost of a source with Tilt: turned 3 degrees. |
tb-drop-target--hover |
A target under the pointer that takes the data: --tb-color-accent border and --tb-color-surface-hover background. The border shows only when the target has a border width. |
tb-drop-target--denied |
A target under the pointer that does not take the data: --tb-color-danger border. |
tb-drop-insert |
The line at the place of the drop in a list target: --tb-color-accent background. |
These are helpers, not controls. They add drag and drop to elements you already have.