3 Answers, 1 is accepted
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;
}
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