void IDropTarget.Drop(IDropInfo dropInfo) { var sourceItem = (SearchCriteria)dropInfo.Data; var targetItem = (SearchCriteria)dropInfo.TargetItem; var ii = dropInfo.InsertIndex; var ip = dropInfo.InsertPosition; // Change source item's group sourceItem.Group = targetItem.Group; // Try to figure out the other item to insert between, or pick a boundary var options = ListSearches .Where(sc => sc != sourceItem) .Where(sc => sc.Group == targetItem.Group) .OrderBy(sc => sc.Order) .ToArray(); // Make no changes if it was dropped on itself if (options.Length == 0) { return; } var above = options.Where(sc => sc.Order < targetItem.Order).OrderByDescending(sc => sc.Order).FirstOrDefault(); var below = options.Where(sc => sc.Order > targetItem.Order).OrderBy(sc => sc.Order).FirstOrDefault(); var aboveOrder = (above == null) ? options.Min(sc => sc.Order) - 1 : above.Order; var belowOrder = (below == null) ? options.Max(sc => sc.Order) + 1 : below.Order; // Update the order to be mid-way between either above or below, based on drag insert position sourceItem.Order = (targetItem.Order + (ip.HasFlag(RelativeInsertPosition.AfterTargetItem) ? belowOrder : aboveOrder)) / 2; // Renumber the results var orderedSearches = ListSearches .OrderBy(sc => sc.Group) .ThenBy(sc => sc.Order) .ToArray(); for (var i = 0; i < orderedSearches.Length; i++) { orderedSearches[i].Order = i; } // Force binding update CollectionViewSource.GetDefaultView(searchesList.ItemsSource).Refresh(); // Save list MarkSearchesChanged(); }
void IDropTarget.Drop(IDropInfo dropInfo) { var sourceItem = (SearchCriteria)dropInfo.Data; var targetItem = (SearchCriteria)dropInfo.TargetItem; var ii = dropInfo.InsertIndex; var ip = dropInfo.InsertPosition; // Change source item's group sourceItem.Group = targetItem.Group; // Try to figure out the other item to insert between, or pick a boundary var options = ListSearches .OrderBy(sc => sc.Group).ThenBy(sc => sc.Order).ThenBy(sc => sc.Species).ThenBy(sc => sc.MinLevel).ThenBy(sc => sc.MaxLevel) .Where(sc => (ip == RelativeInsertPosition.BeforeTargetItem) ? sc.Order <targetItem.Order : sc.Order> targetItem.Order).ToArray(); double bound; if (options.Length > 0) { var otherItem = (ip == RelativeInsertPosition.BeforeTargetItem) ? options.Last() : options.First(); bound = otherItem.Order; } else { bound = targetItem.Order + ((ip == RelativeInsertPosition.BeforeTargetItem) ? -100 : 100); } // Update the order to be mid-way between the two sourceItem.Order = (targetItem.Order + bound) / 2; // Force binding update CollectionViewSource.GetDefaultView(searchesList.ItemsSource).Refresh(); // Save list MarkSearchesChanged(); }