|
| 1 | +using System; |
| 2 | +using System.Threading.Tasks; |
| 3 | +using Avalonia.Controls; |
| 4 | +using Avalonia.Interactivity; |
| 5 | +using Avalonia.Xaml.Interactivity; |
| 6 | + |
| 7 | +//https://github.com/AvaloniaUI/Avalonia/discussions/13301 |
| 8 | +namespace CloudlogHelper.Behaviours |
| 9 | +{ |
| 10 | + public class AutoCompleteZeroMinimumPrefixLengthDropdownBehaviour : Behavior<AutoCompleteBox> |
| 11 | + { |
| 12 | + static AutoCompleteZeroMinimumPrefixLengthDropdownBehaviour() |
| 13 | + { |
| 14 | + } |
| 15 | + |
| 16 | + protected override void OnAttached() |
| 17 | + { |
| 18 | + if (AssociatedObject is not null) |
| 19 | + { |
| 20 | + AssociatedObject.KeyUp += OnKeyUp; |
| 21 | + AssociatedObject.DropDownOpening += DropDownOpening; |
| 22 | + AssociatedObject.GotFocus += OnGotFocus; |
| 23 | + |
| 24 | + Task.Delay(500).ContinueWith(_ => Avalonia.Threading.Dispatcher.UIThread.Invoke(() => { CreateDropdownButton(); })); |
| 25 | + } |
| 26 | + |
| 27 | + base.OnAttached(); |
| 28 | + } |
| 29 | + |
| 30 | + protected override void OnDetaching() |
| 31 | + { |
| 32 | + if (AssociatedObject is not null) |
| 33 | + { |
| 34 | + AssociatedObject.KeyUp -= OnKeyUp; |
| 35 | + AssociatedObject.DropDownOpening -= DropDownOpening; |
| 36 | + AssociatedObject.GotFocus -= OnGotFocus; |
| 37 | + } |
| 38 | + |
| 39 | + base.OnDetaching(); |
| 40 | + } |
| 41 | + |
| 42 | + //have to use KeyUp as AutoCompleteBox eats some of the KeyDown events |
| 43 | + private void OnKeyUp(object? sender, Avalonia.Input.KeyEventArgs e) |
| 44 | + { |
| 45 | + if ((e.Key == Avalonia.Input.Key.Down || e.Key == Avalonia.Input.Key.F4)) |
| 46 | + { |
| 47 | + if (string.IsNullOrEmpty(AssociatedObject?.Text)) |
| 48 | + { |
| 49 | + ShowDropdown(); |
| 50 | + } |
| 51 | + } |
| 52 | + } |
| 53 | + |
| 54 | + private void DropDownOpening(object? sender, System.ComponentModel.CancelEventArgs e) |
| 55 | + { |
| 56 | + var prop = AssociatedObject?.GetType().GetProperty("TextBox", System.Reflection.BindingFlags.Instance | System.Reflection.BindingFlags.NonPublic); |
| 57 | + var tb = (TextBox?)prop?.GetValue(AssociatedObject); |
| 58 | + if (tb is not null && tb.IsReadOnly) |
| 59 | + { |
| 60 | + e.Cancel = true; |
| 61 | + return; |
| 62 | + } |
| 63 | + } |
| 64 | + |
| 65 | + private void ShowDropdown() |
| 66 | + { |
| 67 | + if (AssociatedObject is not null && !AssociatedObject.IsDropDownOpen) |
| 68 | + { |
| 69 | + typeof(AutoCompleteBox).GetMethod("PopulateDropDown", System.Reflection.BindingFlags.NonPublic | System.Reflection.BindingFlags.Instance)?.Invoke(AssociatedObject, new object[] { AssociatedObject, EventArgs.Empty }); |
| 70 | + typeof(AutoCompleteBox).GetMethod("OpeningDropDown", System.Reflection.BindingFlags.NonPublic | System.Reflection.BindingFlags.Instance)?.Invoke(AssociatedObject, new object[] { false }); |
| 71 | + |
| 72 | + if (!AssociatedObject.IsDropDownOpen) |
| 73 | + { |
| 74 | + //We *must* set the field and not the property as we need to avoid the changed event being raised (which prevents the dropdown opening). |
| 75 | + var ipc = typeof(AutoCompleteBox).GetField("_ignorePropertyChange", System.Reflection.BindingFlags.NonPublic | System.Reflection.BindingFlags.Instance); |
| 76 | + if ((bool)ipc?.GetValue(AssociatedObject) == false) |
| 77 | + ipc?.SetValue(AssociatedObject, true); |
| 78 | + |
| 79 | + AssociatedObject.SetCurrentValue<bool>(AutoCompleteBox.IsDropDownOpenProperty, true); |
| 80 | + } |
| 81 | + } |
| 82 | + } |
| 83 | + |
| 84 | + private void CreateDropdownButton() |
| 85 | + { |
| 86 | + if (AssociatedObject != null) |
| 87 | + { |
| 88 | + var prop = AssociatedObject.GetType().GetProperty("TextBox", System.Reflection.BindingFlags.Instance | System.Reflection.BindingFlags.NonPublic); |
| 89 | + var tb = (TextBox?)prop?.GetValue(AssociatedObject); |
| 90 | + if (tb is not null && tb.InnerRightContent is not Button) |
| 91 | + { |
| 92 | + var btn = new Button() |
| 93 | + { |
| 94 | + /* grab symbol from https://www.amp-what.com/unicode/search/down */ |
| 95 | + Content = "⯆", |
| 96 | + Margin = new(3), |
| 97 | + ClickMode = ClickMode.Press |
| 98 | + }; |
| 99 | + btn.Click += (s, e) => |
| 100 | + { |
| 101 | + AssociatedObject.Text = string.Empty; |
| 102 | + ShowDropdown(); |
| 103 | + }; |
| 104 | + |
| 105 | + tb.InnerRightContent = btn; |
| 106 | + } |
| 107 | + } |
| 108 | + } |
| 109 | + |
| 110 | + private void OnGotFocus(object? sender, RoutedEventArgs e) |
| 111 | + { |
| 112 | + if (AssociatedObject != null) |
| 113 | + { |
| 114 | + CreateDropdownButton(); |
| 115 | + } |
| 116 | + } |
| 117 | + } |
| 118 | +} |
0 commit comments