1
votes

I'm using the gong-wpf libary for reordering items via drag/drop in a databound listBox:

<ListBox ItemsSource="{Binding Collection}"
 dd:DragDrop.IsDragSource="True"
 dd:DragDrop.IsDropTarget="True" />

Is there a simple solution that the dragged item keeps its IsSelected value when dropped to the new position? Unfortunately every drag/drop operation clears the listBox SelectedItem.

I've already thought about implementing the IDropTarget interface and then doing this in the drop handler. But in this case I would have to implement the reordering logic completely myself. Is there really no simpler solution?

1
If you implement the IDragHandler, gong-wpf will use the "DefaultDropTarget" which handles the re-ordering for you. You still have a callback when the drop is successful.Berin Loritsch
I'm sorry, but I only found the interfaces IDragInfo, IDragSource, IDropInfo, IDropTarget in the libary namespace. Where can I find IDragHandler?User95
I think I have a really old version. It probably got renamed to IDropTarget.Berin Loritsch
I've already tried to implement the IDropTarget interface containing DragOver(...) and Drop(...). Implementing DragOver(...) is no problem, but in Drop(...) I don't know a workaround for programming the reordering logic myself. Furthermore I tried to implement the reordering logic, but dropInfo.InsertPosition provides me sometimes wrong values.User95

1 Answers

1
votes

Try adding PreviewMouseLeftButtonDown event.

Like this,

public DragDropWindow()
{
    Style itemContainerStyle = new Style(typeof(ListBoxItem));
    itemContainerStyle.Setters.Add(new Setter(ListBoxItem.AllowDropProperty, true));
    itemContainerStyle.Setters.Add(new EventSetter(ListBoxItem.PreviewMouseLeftButtonDownEvent, new MouseButtonEventHandler(s_PreviewMouseLeftButtonDown)));
    itemContainerStyle.Setters.Add(new EventSetter(ListBoxItem.PreviewMouseMoveEvent, new MouseEventHandler(s_PreviewMouseMoveEvent)));
    listbox1.ItemContainerStyle = itemContainerStyle;
}

Drag and drop process

void s_PreviewMouseLeftButtonDown(object sender, MouseButtonEventArgs e)
{
     if (sender is ListBoxItem)
      {
         ListBoxItem draggedItem = sender as ListBoxItem;
         DragDrop.DoDragDrop(draggedItem, draggedItem.DataContext, DragDropEffects.Move);
         draggedItem.IsSelected = true;
      }
}

Or

Add PreviewMouseMoveEvent

Like this,

void s_PreviewMouseMoveEvent(object sender, MouseEventArgs e)
    {
         if (sender is ListBoxItem && e.LeftButton == MouseButtonState.Pressed)
          {
             ListBoxItem draggedItem = sender as ListBoxItem;
             DragDrop.DoDragDrop(draggedItem, draggedItem.DataContext, DragDropEffects.Move);
             draggedItem.IsSelected = true;
          }

    }

The above code is a base theme which you can change according to your requirement. Either it can be inside the control or out side.

For more reference see here