// Performs custom drop logic for the top ListView. void dragMgr_ProcessDrop(object sender, ProcessDropEventArgs <Path> e) { // This shows how to customize the behavior of a drop. // Here we perform a swap, instead of just moving the dropped item. int higherIdx = Math.Max(e.OldIndex, e.NewIndex); int lowerIdx = Math.Min(e.OldIndex, e.NewIndex); if (lowerIdx < 0) { // The item came from the lower ListView // so just insert it. e.ItemsSource.Insert(higherIdx, e.DataItem); } else { // null values will cause an error when calling Move. // It looks like a bug in ObservableCollection to me. if (e.ItemsSource[lowerIdx] == null || e.ItemsSource[higherIdx] == null) { return; } // The item came from the ListView into which // it was dropped, so swap it with the item // at the target index. e.ItemsSource.Move(lowerIdx, higherIdx); e.ItemsSource.Move(higherIdx - 1, lowerIdx); } // Set this to 'Move' so that the OnListViewDrop knows to // remove the item from the other ListView. e.Effects = DragDropEffects.Move; var obsCollection = (IEnumerable <Path>) this.listViewFavorites.ItemsSource; FavoritePaths.Instance.SetFavorites(new List <Path>(obsCollection)); }
void listView_Drop(object sender, DragEventArgs e) { if (this.ItemUnderDragCursor != null) { this.ItemUnderDragCursor = null; } e.Effects = DragDropEffects.None; if (!e.Data.GetDataPresent(typeof(ItemType))) { return; } // Get the data object which was dropped. ItemType data = e.Data.GetData(typeof(ItemType)) as ItemType; if (data == null) { return; } // Get the ObservableCollection<ItemType> which contains the dropped data object. ObservableCollection <ItemType> itemsSource = this.listView.ItemsSource as ObservableCollection <ItemType>; if (itemsSource == null) { throw new Exception( "A ListView managed by ListViewDragManager must have its ItemsSource set to an ObservableCollection<ItemType>."); } int oldIndex = itemsSource.IndexOf(data); int newIndex = this.IndexUnderDragCursor; if (newIndex < 0) { // The drag started somewhere else, and our ListView is empty // so make the new item the first in the list. if (itemsSource.Count == 0) { newIndex = 0; } // The drag started somewhere else, but our ListView has items // so make the new item the last in the list. else if (oldIndex < 0) { newIndex = itemsSource.Count; } // The user is trying to drop an item from our ListView into // our ListView, but the mouse is not over an item, so don't // let them drop it. else { return; } } // Dropping an item back onto itself is not considered an actual 'drop'. if (oldIndex == newIndex) { return; } if (this.ProcessDrop != null) { // Let the client code process the drop. ProcessDropEventArgs <ItemType> args = new ProcessDropEventArgs <ItemType>(itemsSource, data, oldIndex, newIndex, e.AllowedEffects); this.ProcessDrop(this, args); e.Effects = args.Effects; } else { // Move the dragged data object from it's original index to the // new index (according to where the mouse cursor is). If it was // not previously in the ListBox, then insert the item. if (oldIndex > -1) { itemsSource.Move(oldIndex, newIndex); } else { itemsSource.Insert(newIndex, data); } // Set the Effects property so that the call to DoDragDrop will return 'Move'. e.Effects = DragDropEffects.Move; } }