You can implement drag-and-drop on a touch-based system in a similar way to how you implement drag-and-drop using the mouse.
You start by defining a handler for the TouchDown event of the control that serves as a drag source. You then define a handler for the Drop event of the control that is the drop target.
<StackPanel> <Label Content="Benjamin Disraeli" Background="AliceBlue" Margin="15" Padding="30,20" HorizontalAlignment="Center" TouchDown="Label_TouchDown"/> <Label Content="Drag to here" Background="Bisque" Margin="15" Padding="30,20" HorizontalAlignment="Center" AllowDrop="True" Drop="Label_Drop"/> </StackPanel>
In the code-behind, you call the DoDragDrop method to initiate drag-and-drop.
// Drag source private void Label_TouchDown(object sender, TouchEventArgs e) { Label l = e.Source as Label; DragDrop.DoDragDrop(l, l.Content + " was Dragged!", DragDropEffects.Copy); } // Drag target private void Label_Drop(object sender, DragEventArgs e) { string draggedText = (string)e.Data.GetData(DataFormats.StringFormat); Label l = e.Source as Label; l.Content = draggedText; }
You can now touch and hold your finger down to drag.
Filed under: Events Tagged: Drag-and-Drop, Events, Touch, TouchDown, WPF
