|
| 1 | +using System.Windows; |
| 2 | +using System.Windows.Controls; |
| 3 | +using System.Windows.Input; |
| 4 | +using System.Windows.Media; |
| 5 | + |
| 6 | +namespace Tender.Desktop.Helpers; |
| 7 | + |
| 8 | +/// <summary> |
| 9 | +/// 為 ListBox 加上 drag-and-drop 重新排序能力。 |
| 10 | +/// 用法:<c>ListBoxReorderHelper.Attach(myListBox, (from, to) => vm.Move(from, to));</c> |
| 11 | +/// 真正的集合操作交給 callback;helper 不假設 ItemsSource 是哪種集合,只算 index。 |
| 12 | +/// </summary> |
| 13 | +public static class ListBoxReorderHelper |
| 14 | +{ |
| 15 | + private const double DragThreshold = 6.0; |
| 16 | + |
| 17 | + private sealed class State |
| 18 | + { |
| 19 | + public ListBox ListBox = null!; |
| 20 | + public Action<int, int> OnReorder = null!; |
| 21 | + public Point StartPoint; |
| 22 | + public int DragStartIndex = -1; |
| 23 | + } |
| 24 | + |
| 25 | + public static void Attach(ListBox listBox, Action<int, int> onReorder) |
| 26 | + { |
| 27 | + var state = new State { ListBox = listBox, OnReorder = onReorder }; |
| 28 | + listBox.AllowDrop = true; |
| 29 | + listBox.PreviewMouseLeftButtonDown += (s, e) => OnMouseDown(state, e); |
| 30 | + listBox.PreviewMouseMove += (s, e) => OnMouseMove(state, e); |
| 31 | + listBox.Drop += (s, e) => OnDrop(state, e); |
| 32 | + listBox.DragOver += (s, e) => |
| 33 | + { |
| 34 | + e.Effects = e.Data.GetDataPresent(typeof(int)) ? DragDropEffects.Move : DragDropEffects.None; |
| 35 | + e.Handled = true; |
| 36 | + }; |
| 37 | + } |
| 38 | + |
| 39 | + private static void OnMouseDown(State state, MouseButtonEventArgs e) |
| 40 | + { |
| 41 | + // 點擊時記錄起始 index;交給 ListBox 本身先處理 selection,等 MouseMove 才啟動 drag |
| 42 | + var item = FindAncestor<ListBoxItem>(e.OriginalSource as DependencyObject); |
| 43 | + if (item is null) return; |
| 44 | + state.StartPoint = e.GetPosition(state.ListBox); |
| 45 | + state.DragStartIndex = state.ListBox.ItemContainerGenerator.IndexFromContainer(item); |
| 46 | + } |
| 47 | + |
| 48 | + private static void OnMouseMove(State state, MouseEventArgs e) |
| 49 | + { |
| 50 | + if (e.LeftButton != MouseButtonState.Pressed || state.DragStartIndex < 0) return; |
| 51 | + var pos = e.GetPosition(state.ListBox); |
| 52 | + if (Math.Abs(pos.X - state.StartPoint.X) < DragThreshold && |
| 53 | + Math.Abs(pos.Y - state.StartPoint.Y) < DragThreshold) return; |
| 54 | + |
| 55 | + // 啟動 drag-drop;payload 放 source index |
| 56 | + try |
| 57 | + { |
| 58 | + DragDrop.DoDragDrop(state.ListBox, state.DragStartIndex, DragDropEffects.Move); |
| 59 | + } |
| 60 | + finally |
| 61 | + { |
| 62 | + state.DragStartIndex = -1; |
| 63 | + } |
| 64 | + } |
| 65 | + |
| 66 | + private static void OnDrop(State state, DragEventArgs e) |
| 67 | + { |
| 68 | + if (!e.Data.GetDataPresent(typeof(int))) return; |
| 69 | + var from = (int)e.Data.GetData(typeof(int)); |
| 70 | + |
| 71 | + // 找到 drop 目標的 index:游標下方的 ListBoxItem;若 drop 到空白處則放最末 |
| 72 | + var targetItem = FindAncestor<ListBoxItem>(e.OriginalSource as DependencyObject); |
| 73 | + int to; |
| 74 | + if (targetItem is not null) |
| 75 | + { |
| 76 | + to = state.ListBox.ItemContainerGenerator.IndexFromContainer(targetItem); |
| 77 | + } |
| 78 | + else |
| 79 | + { |
| 80 | + to = state.ListBox.Items.Count - 1; |
| 81 | + } |
| 82 | + if (to < 0 || from == to) return; |
| 83 | + state.OnReorder(from, to); |
| 84 | + e.Handled = true; |
| 85 | + } |
| 86 | + |
| 87 | + private static T? FindAncestor<T>(DependencyObject? d) where T : DependencyObject |
| 88 | + { |
| 89 | + while (d != null) |
| 90 | + { |
| 91 | + if (d is T t) return t; |
| 92 | + d = VisualTreeHelper.GetParent(d); |
| 93 | + } |
| 94 | + return null; |
| 95 | + } |
| 96 | +} |
0 commit comments