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

How to reorder the items in RadListbox?

3 Answers 84 Views
DragAndDrop
This is a migrated thread and some comments may be shown as answers.
Uli
Top achievements
Rank 1
Veteran
Uli asked on 18 Nov 2019, 04:59 PM
I want to reorder the  the items in RadListbox thru DragDropManager. Any example?

3 Answers, 1 is accepted

Sort by
0
Uli
Top achievements
Rank 1
Veteran
answered on 19 Nov 2019, 09:08 AM
e.g. drag the 4th item to the 6th position or to the last position
0
Uli
Top achievements
Rank 1
Veteran
answered on 19 Nov 2019, 04:06 PM

Now I created following codes as solution. But I don't know if it is the best.

        private static void OnDrop(object sender, Telerik.Windows.DragDrop.DragEventArgs e)
        {
            var data = DragDropPayloadManager.GetDataFromObject(e.Data, "DragData");
            var itemsSource = (IList)(sender as RadListBox).ItemsSource;
            var ziel = ((FrameworkElement)e.OriginalSource).DataContext;
            var oriIndex = (itemsSource).IndexOf(data);
            var destinationIndex = (itemsSource).IndexOf(ziel);
            int insertIndex;
            if (e.OriginalSource.GetType().Name == "Grid")
            {
                insertIndex = itemsSource.Count - 1;
            }
            else if (e.OriginalSource.GetType().Name == "Border")
            {
                insertIndex = 0;
            }
            else
            {
                Point pt = e.GetPosition((TextBlock)e.OriginalSource);
                insertIndex = pt.Y > ((TextBlock)e.OriginalSource).ActualHeight / 2 ? destinationIndex + 1 : destinationIndex;
            }
            itemsSource.Remove(data);
            if (insertIndex > itemsSource.Count)
                insertIndex = itemsSource.Count;
            else if (oriIndex < destinationIndex)
                insertIndex--;
            itemsSource.Insert(insertIndex, data);
            e.Handled = true;
        }

        private static void OnDragInitialize(object sender, DragInitializeEventArgs e)
        {
            e.AllowedEffects = DragDropEffects.All;
            var payload = DragDropPayloadManager.GeneratePayload(null);
            var data = ((FrameworkElement)e.OriginalSource).DataContext;
            payload.SetData("DragData", data);
            e.Data = payload;
            e.Handled = true;
        }

0
Dilyan Traykov
Telerik team
answered on 21 Nov 2019, 10:03 AM

Hello Uli,

The RadListBox provides an out-of-the-box solution for enabling drag-drop operations - the ListBoxDragDropBehavior.

Can you please give this a try and let me know if it works for you?

Regards,
Dilyan Traykov
Progress Telerik

Get quickly onboarded and successful with your Telerik and/or Kendo UI products with the Virtual Classroom free technical training, available to all active customers. Learn More.
Tags
DragAndDrop
Asked by
Uli
Top achievements
Rank 1
Veteran
Answers by
Uli
Top achievements
Rank 1
Veteran
Dilyan Traykov
Telerik team
Share this question
or