|
| 1 | +using System.Runtime.CompilerServices; |
| 2 | +using System.Windows; |
| 3 | +using System.Windows.Controls; |
| 4 | +using System.Windows.Controls.Primitives; |
| 5 | + |
| 6 | +namespace HandyControl.Controls; |
| 7 | + |
| 8 | +public class SelectAllButtonAttach |
| 9 | +{ |
| 10 | + private static readonly ConditionalWeakTable<Selector, ToggleButton> SelectorButtonTable = new(); |
| 11 | + |
| 12 | + public static readonly DependencyProperty TargetProperty = DependencyProperty.RegisterAttached( |
| 13 | + "Target", typeof(Selector), typeof(SelectAllButtonAttach), new PropertyMetadata(null, OnTargetChanged)); |
| 14 | + |
| 15 | + public static void SetTarget(DependencyObject element, Selector value) |
| 16 | + => element.SetValue(TargetProperty, value); |
| 17 | + |
| 18 | + public static Selector GetTarget(DependencyObject element) |
| 19 | + => (Selector) element.GetValue(TargetProperty); |
| 20 | + |
| 21 | + private static void OnTargetChanged(DependencyObject d, DependencyPropertyChangedEventArgs e) |
| 22 | + { |
| 23 | + if (d is not ToggleButton button) |
| 24 | + { |
| 25 | + return; |
| 26 | + } |
| 27 | + |
| 28 | + button.Click -= OnButtonOnClick; |
| 29 | + |
| 30 | + if (e.OldValue is Selector oldSelector) |
| 31 | + { |
| 32 | + oldSelector.SelectionChanged -= OnSelectorSelectionChanged; |
| 33 | + SelectorButtonTable.Remove(oldSelector); |
| 34 | + } |
| 35 | + |
| 36 | + if (e.NewValue is not Selector newSelector) |
| 37 | + { |
| 38 | + return; |
| 39 | + } |
| 40 | + |
| 41 | + button.Click += OnButtonOnClick; |
| 42 | + newSelector.SelectionChanged += OnSelectorSelectionChanged; |
| 43 | + |
| 44 | + if (!SelectorButtonTable.TryGetValue(newSelector, out _)) |
| 45 | + { |
| 46 | + SelectorButtonTable.Add(newSelector, button); |
| 47 | + } |
| 48 | + } |
| 49 | + |
| 50 | + private static void OnButtonOnClick(object sender, RoutedEventArgs e) |
| 51 | + { |
| 52 | + if (sender is not ToggleButton button) |
| 53 | + { |
| 54 | + return; |
| 55 | + } |
| 56 | + |
| 57 | + if (button.IsChecked == true) |
| 58 | + { |
| 59 | + SelectAll(GetTarget(button)); |
| 60 | + } |
| 61 | + else |
| 62 | + { |
| 63 | + UnselectAll(GetTarget(button)); |
| 64 | + } |
| 65 | + } |
| 66 | + |
| 67 | + private static void OnSelectorSelectionChanged(object sender, SelectionChangedEventArgs e) |
| 68 | + { |
| 69 | + if (sender is not Selector selector) |
| 70 | + { |
| 71 | + return; |
| 72 | + } |
| 73 | + |
| 74 | + if (!SelectorButtonTable.TryGetValue(selector, out var button)) |
| 75 | + { |
| 76 | + return; |
| 77 | + } |
| 78 | + |
| 79 | + var selectedCount = GetSelectedItemsCount(selector); |
| 80 | + var totalCount = selector.Items.Count; |
| 81 | + button.IsChecked = selectedCount switch |
| 82 | + { |
| 83 | + 0 => false, |
| 84 | + _ when selectedCount == totalCount => true, |
| 85 | + _ => null // Indeterminate |
| 86 | + }; |
| 87 | + } |
| 88 | + |
| 89 | + private static void SelectAll(Selector selector) |
| 90 | + { |
| 91 | + if (selector is MultiSelector multiSelector) |
| 92 | + { |
| 93 | + multiSelector.SelectAll(); |
| 94 | + } |
| 95 | + else if (selector is ListBox listBox) |
| 96 | + { |
| 97 | + listBox.SelectAll(); |
| 98 | + } |
| 99 | + } |
| 100 | + |
| 101 | + private static void UnselectAll(Selector selector) |
| 102 | + { |
| 103 | + if (selector is MultiSelector multiSelector) |
| 104 | + { |
| 105 | + multiSelector.UnselectAll(); |
| 106 | + } |
| 107 | + else if (selector is ListBox listBox) |
| 108 | + { |
| 109 | + listBox.UnselectAll(); |
| 110 | + } |
| 111 | + } |
| 112 | + |
| 113 | + private static int GetSelectedItemsCount(Selector selector) |
| 114 | + { |
| 115 | + if (selector is MultiSelector multiSelector) |
| 116 | + { |
| 117 | + return multiSelector.SelectedItems.Count; |
| 118 | + } |
| 119 | + |
| 120 | + if (selector is ListBox listBox) |
| 121 | + { |
| 122 | + return listBox.SelectedItems.Count; |
| 123 | + } |
| 124 | + |
| 125 | + return 0; |
| 126 | + } |
| 127 | +} |
0 commit comments