Keep Item In List after drag/drop

2 posts, 0 answers
  1. heavywoody
    heavywoody avatar
    79 posts
    Member since:
    Oct 2009

    Posted 02 Jul 2013 Link to this post

    I am using two RadListBoxes as well as a ItemCollection.  The are bound to observable collections of business objects.    When I drag items between them, I don't want the item to be removed from the collection, but rather just a copy placed where it was dragged to.  How would I keep the items in the list?   It seems no matter what I do, the item is removed from the list that it was drug from.
  2. heavywoody
    heavywoody avatar
    79 posts
    Member since:
    Oct 2009

    Posted 02 Jul 2013 Link to this post

    I think I figured it out by looking at different examples.  Inheriting the ListBoxDragDropBehavior is the key.   Maybe I am missing the source, but I couldn't find much documentation on it, which would be very helpful.  But based on a same you had, I played around until I got it to do what I wanted.  SO this does three things:

    1) Won't remove an item from a source listbox
    2) Won't allow use to drag/drop into the same listbox
    3) Won't allow a duplicate to be dropped into a listbox

    public class CustomDragDropBehavior : ListBoxDragDropBehavior
    {
    public override void Drop(DragDropState state)
    {
                if(!state.IsSameControl)
                {
       foreach (object o in state.DraggedItems)
       {
                        if(!state.DestinationItemsSource.Contains(o))
                        state.DestinationItemsSource.Add(o);
                    }
                }
    //base.Drop(state);
    }

    public override void DragDropCompleted(DragDropState state)
    {
                foreach (var item in state.DraggedItems)
                {
                   // state.SourceItemsSource.Remove(item);
                }
               // base.DragDropCompleted(state);
    }

            public override bool CanDrop(Telerik.Windows.DragDrop.Behaviors.DragDropState state)
            {
                return !state.IsSameControl;
            }

    protected override bool IsMovingItems(DragDropState state)
    {
    return !state.IsSameControl;
    }
  3. UI for WPF is Visual Studio 2017 Ready
Back to Top