1
votes

I have a WPF app which contains a listbox and a few items. In between I have also added dummy items so that I can drag and drop between these items.

I have implemented the drag and drop and it works perfectly with the mouse. With touch it works, but not the way I desire it to. When listbox items is not selected, a drag and drop using touch works but without any drag and drop effect, I guess I will have to create an Adorner for that. But when a listbox item is selected, the drag and drop doesn't work. I have to manually unselect the item and then it works. Even the UnSelectAll() method does no good.

I just wanted to know how exactly should drag and drop be implemented for touch devices?

The code snippets are as follows:

private void s_PreviewMouseDown(object sender, MouseButtonEventArgs e)
{
    if (sender is ListBoxItem)
    {
        ListBoxItem draggedItem = sender as ListBoxItem;
        System.Windows.DragDrop.DoDragDrop(draggedItem, draggedItem.DataContext, DragDropEffects.Move);
        draggedItem.IsSelected = true;
    }
}
private void s_PreviewTouchDown(object sender, TouchEventArgs e)
{
    if (sender is ListBoxItem)
    {
        ListBoxItem draggedItem = sender as ListBoxItem;
        System.Windows.DragDrop.DoDragDrop(draggedItem, draggedItem.DataContext, DragDropEffects.Move);
        draggedItem.IsSelected = true;
        e.Handled = true;
    }
}
private void listbox1_Drop(object sender, DragEventArgs e)
{
    ItemTemplate droppedData = e.Data.GetData(typeof(ItemTemplate)) as ItemTemplate;
    ItemTemplate target = ((ListBoxItem)(sender)).DataContext as ItemTemplate;

    int removedIdx = listBox1.Items.IndexOf(droppedData);
    int targetIdx = listBox1.Items.IndexOf(target);

    Swap<ItemTemplate>(AppsList, targetIdx, removedIdx);
}

Any help would be appreciated. Is the Listbox in WPF having issues with touch?

1

1 Answers

0
votes

No. You cannot use touch events properly with the generic WPF ListBox. Try using Surface listBox. SurfaceListBox