This is a migrated thread and some comments may be shown as answers.

ListBoxDragDropBehavior problem

6 Answers 226 Views
DragAndDrop
This is a migrated thread and some comments may be shown as answers.
Michael
Top achievements
Rank 1
Michael asked on 20 Feb 2012, 10:40 PM
I have been trying to implement DnD between ListBox controls in WPF and also reordering items in the list box. 
The situation that I have is 3 listboxes, one is the "Toolbox" which contains all of the objects that will ever be dragged, one which they get dragged onto, called the "Workfow", and another one called the "Output" which objects from the "Workflow" get dragged from. When dragging, all objects should be copied rather than moved across.  The "Toolbox" and "Workflow" listboxes are bound to lists of "Element" objects, and the "Output" listbox is bound to a list of KeyValuePair<string, string> objects which gets populated when the element is dropped from the "Workflow" listbox.  A bit complicated I know.

Dragging from "Toolbox" to "Workflow" works fine, and reordering items in "Workflow" works too, but when I try and drag from "Workflow" to "Output" the DragDropState.DraggedItems property is always empty.  

Below is my override of the ListBoxDragDropBehavior

public class WfBehavior : ListBoxDragDropBehavior
    {
        public override void Drop(DragDropState state)
        {
            foreach (object o in state.DraggedItems)
            {
                if (Dropped != null)
                {
                    Dropped(o as Element, state.InsertIndex);
                }
            }
            base.Drop(state);
        }
 
        protected override bool IsMovingItems(DragDropState state)
        {
            return state.IsSameControl;
        }
 
        public static event DroppedEventHandler Dropped;
    }
 
    public delegate void DroppedEventHandler(Element element, int index);

6 Answers, 1 is accepted

Sort by
0
Nick
Telerik team
answered on 21 Feb 2012, 04:13 PM
Hello Michael,

Since the Source items of the Workflow and Output ListBoxes are of different type, you have to define your own DataConverter in order to be able to do a successful DragDropOperation. You can see how it can be implemented here. I am attaching the project I worked with for your reference. The base functionality is implemented, so you shouldn't have any problem developing it further to suit your requirements.

Hope this helps!

Regards,
Nik
the Telerik team
Sharpen your .NET Ninja skills! Attend Q1 webinar week and get a chance to win a license! Book your seat now >>
0
Michael
Top achievements
Rank 1
answered on 21 Feb 2012, 09:35 PM
Hi Nik,

Thanks for your reply.  I have implemented this DataConverter:
public class MyDataConverter : DataConverter
{
    public override string[] GetConvertToFormats()
    {
        return new string[] { typeof(Element).FullName, typeof(string).FullName };
    }
 
    public override object ConvertTo(object data, string format)
    {
        if(format == typeof(Element).FullName && DataObjectHelper.GetDataPresent(data, typeof(Element), false))
        {
            var elements = (IEnumerable)DataObjectHelper.GetData(data, typeof(Element),false);
            if(elements != null)
                return new ThreeShotHeightTask();
        }
        else if (format == typeof(string).FullName)
        {
            return "test";
        }
        return null;
    }
 
}

This does not solve the problem though.  The DraggedItems property of the DragDropState is still an empty array of objects and I can't drop onto the Output listbox. I have changed the ItemsSource of the Output Listbox to a ObservableCollection<string> for testing.
0
Accepted
Nick
Telerik team
answered on 22 Feb 2012, 10:40 AM
Hello,

By default the DragDropBehavior expects a collection for the DraggedItems. Therefore, you should return an IEnumerable<string> in the DataConverter. 

if (format == typeof(String).FullName)
            {
                return new string[] { "empty" };
            }
You can see the exact implementation in the sample attached to my previous post.

All the best,
Nik
the Telerik team
Sharpen your .NET Ninja skills! Attend Q1 webinar week and get a chance to win a license! Book your seat now >>
0
Michael
Top achievements
Rank 1
answered on 22 Feb 2012, 09:18 PM
Ahh that explains a lot! Thanks for that Nik.
0
Arnas
Top achievements
Rank 1
answered on 13 May 2013, 06:43 PM
Hi,

This sample produces "empty" element every time MyViewModel is dragged onto ListBox3.
How to get actual MyViewModel, which was dragged (So it can be converted to String) ??? 

On 2013.1.403.40

Thanks,
Arnas

0
Nick
Telerik team
answered on 14 May 2013, 01:10 PM
Hello Amas,

You should use the data parameter of the Convert method to access the dragged data. You can then use it to return whatever you need for your target Element.

hope this helps! 

Regards,
Nik
the Telerik team

Explore the entire Telerik portfolio by downloading Telerik DevCraft Ultimate.

Tags
DragAndDrop
Asked by
Michael
Top achievements
Rank 1
Answers by
Nick
Telerik team
Michael
Top achievements
Rank 1
Arnas
Top achievements
Rank 1
Share this question
or