Начиная с примера кода, который вы связали на другом сайте, я внес некоторые изменения и смог исправить решение для работы с ItemsSource.
Внесены следующие изменения:
- в MainWindow.xaml
<ListView ItemsSource="{Binding List}"
x:Name="listView"
- в MainWindow.xaml.cs
private void ListViewDrop(object sender, DragEventArgs e)
{
if (e.Data.GetDataPresent("myFormat"))
{
MyObject name = e.Data.GetData("myFormat") as MyObject;
ListViewItem listViewItem = FindAnchestor<ListViewItem>((DependencyObject)e.OriginalSource);
ObservableCollection<MyObject> myArray = listView.ItemsSource as ObservableCollection<MyObject>;
if (listViewItem != null && listViewItem.DataContext is MyObject)
{
MyObject dropLocation = (MyObject)listViewItem.DataContext;
int index = myArray.IndexOf(dropLocation);
if (index >= 0)
{
myArray.Remove(name);
myArray.Insert(index, name);
}
}
else
{
myArray.Remove(name);
myArray.Add(name);
}
}
}
...
private void BeginDrag(MouseEventArgs e)
{
ListView listView = this.listView;
ListViewItem listViewItem =
FindAnchestor<ListViewItem>((DependencyObject)e.OriginalSource);
if (listViewItem == null)
return;
MyObject selectedItem = (MyObject)listViewItem.DataContext;
//setup the drag adorner.
InitialiseAdorner(listViewItem);
//add handles to update the adorner.
listView.PreviewDragOver += ListViewDragOver;
listView.DragLeave += ListViewDragLeave;
listView.DragEnter += ListViewDragEnter;
DataObject data = new DataObject("myFormat", selectedItem);
DragDropEffects de = DragDrop.DoDragDrop(this.listView, data, DragDropEffects.Move);
//cleanup
listView.PreviewDragOver -= ListViewDragOver;
listView.DragLeave -= ListViewDragLeave;
listView.DragEnter -= ListViewDragEnter;
if (_adorner != null)
{
AdornerLayer.GetAdornerLayer(listView).Remove(_adorner);
_adorner = null;
}
}
Сами изменения используют концепцию MyObject в качестве полезной нагрузки вместо строки и ObservableCollection в качестве массива, в котором вы вносите изменения, вместо IEnumerable.
Надеюсь, это решит вашу проблему.