ListBoxDragDropBehavior problem

7 posts, 1 answers
  1. Michael
    Michael avatar
    8 posts
    Member since:
    Feb 2012

    Posted 20 Feb 2012 Link to this post

    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);

  2. Nick
    Admin
    Nick avatar
    593 posts

    Posted 21 Feb 2012 Link to this post

    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 >>
  3. UI for WPF is Visual Studio 2017 Ready
  4. Michael
    Michael avatar
    8 posts
    Member since:
    Feb 2012

    Posted 21 Feb 2012 Link to this post

    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.
  5. Answer
    Nick
    Admin
    Nick avatar
    593 posts

    Posted 22 Feb 2012 Link to this post

    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 >>
  6. Michael
    Michael avatar
    8 posts
    Member since:
    Feb 2012

    Posted 22 Feb 2012 Link to this post

    Ahh that explains a lot! Thanks for that Nik.
  7. Arnas
    Arnas avatar
    1 posts
    Member since:
    May 2013

    Posted 13 May 2013 Link to this post

    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

  8. Nick
    Admin
    Nick avatar
    593 posts

    Posted 14 May 2013 Link to this post

    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.

Back to Top
UI for WPF is Visual Studio 2017 Ready