I have ListView
with ItemSource:
public ObservableCollection<MyObject> List;
And my ListView
is full with several object.
Now i want to add the option to change my ListViewItems
via darg so i found this solution: https://fxmax.wordpress.com/2010/10/05/wpf/
And after add the code inside my project i only have one problem that cause crash:
private void BeginDrag(MouseEventArgs e)
{
ListView listView = this.listView;
ListViewItem listViewItem = FindAnchestor<ListViewItem>((DependencyObject)e.OriginalSource);
if (listViewItem == null)
return;
// get the data for the ListViewItem
MyObject name = (MyObject)listView.ItemContainerGenerator.ItemFromContainer(listViewItem);
//setup the drag adorner.
InitialiseAdorner(listViewItem);
//add handles to update the adorner.
listView.PreviewDragOver += listView_PreviewDragOver;
listView.DragLeave += listView_DragLeave;
listView.DragEnter += listView_DragEnter;
DataObject data = new DataObject("myFormat", name);
DragDropEffects de = DragDrop.DoDragDrop(this.listView, data, DragDropEffects.Move);
//cleanup
listView.PreviewDragOver -= listView_PreviewDragOver;
listView.DragLeave -= listView_DragLeave;
listView.DragEnter -= listView_DragEnter;
if (_adorner != null)
{
AdornerLayer.GetAdornerLayer(listView).Remove(_adorner);
_adorner = null;
}
}
At this line:
DragDropEffects de = DragDrop.DoDragDrop(this.listView, data, DragDropEffects.Move);
System.InvalidOperationException: 'Operation is not valid while ItemsSource is in use. Access and modify elements with ItemsControl.ItemsSource instead.'
I can see that is because i am using ItemSource
but dont know what to change.
Any suggestions ?