From 1a9f2e9945d392a29b2c34ddaa17ffbb7923a3a3 Mon Sep 17 00:00:00 2001 From: rabbitism Date: Mon, 15 Sep 2025 10:00:48 +0800 Subject: [PATCH] feat: cleanup code. add missing template parts. --- .../AutoCompleteBox/MultiAutoCompleteBox.cs | 40 +++++++++++-------- 1 file changed, 23 insertions(+), 17 deletions(-) diff --git a/src/Ursa/Controls/AutoCompleteBox/MultiAutoCompleteBox.cs b/src/Ursa/Controls/AutoCompleteBox/MultiAutoCompleteBox.cs index 604a4db..4b66808 100644 --- a/src/Ursa/Controls/AutoCompleteBox/MultiAutoCompleteBox.cs +++ b/src/Ursa/Controls/AutoCompleteBox/MultiAutoCompleteBox.cs @@ -10,6 +10,7 @@ using System.Collections; using Avalonia; using Avalonia.Collections; using Avalonia.Controls; +using Avalonia.Controls.Metadata; using Avalonia.Controls.Primitives; using Avalonia.Controls.Templates; using Avalonia.Controls.Utils; @@ -24,6 +25,11 @@ using Ursa.Common; namespace Ursa.Controls; +[TemplatePart(ElementPopup, typeof(Popup))] +[TemplatePart(ElementSelector, typeof(SelectingItemsControl))] +[TemplatePart(ElementSelectionAdapter, typeof(ISelectionAdapter))] +[TemplatePart(ElementTextBox, typeof(TextBox))] +[PseudoClasses(":dropdownopen")] public partial class MultiAutoCompleteBox : TemplatedControl, IInnerContentControl { /// @@ -176,18 +182,15 @@ public partial class MultiAutoCompleteBox : TemplatedControl, IInnerContentContr { FocusableProperty.OverrideDefaultValue(true); IsTabStopProperty.OverrideDefaultValue(false); - - MinimumPopulateDelayProperty.Changed.AddClassHandler((x, e) => - x.OnMinimumPopulateDelayChanged(e)); - IsDropDownOpenProperty.Changed.AddClassHandler((x, e) => x.OnIsDropDownOpenChanged(e)); - // SelectedItemProperty.Changed.AddClassHandler((x, e) => x.OnSelectedItemPropertyChanged(e)); - TextProperty.Changed.AddClassHandler((x, e) => x.OnTextPropertyChanged(e)); - SearchTextProperty.Changed.AddClassHandler((x, e) => x.OnSearchTextPropertyChanged(e)); - FilterModeProperty.Changed.AddClassHandler((x, e) => x.OnFilterModePropertyChanged(e)); - ItemFilterProperty.Changed.AddClassHandler((x, e) => x.OnItemFilterPropertyChanged(e)); - ItemsSourceProperty.Changed.AddClassHandler((x, e) => x.OnItemsSourcePropertyChanged(e)); - ItemTemplateProperty.Changed.AddClassHandler((x, e) => x.OnItemTemplatePropertyChanged(e)); - IsEnabledProperty.Changed.AddClassHandler((x, e) => x.OnControlIsEnabledChanged(e)); + MinimumPopulateDelayProperty.Changed.AddClassHandler((box, args) => box.OnMinimumPopulateDelayChanged(args)); + IsDropDownOpenProperty.Changed.AddClassHandler((box, args)=> box.OnIsDropDownOpenChanged(args)); + TextProperty.Changed.AddClassHandler((box, args) => box.OnTextPropertyChanged(args)); + SearchTextProperty.Changed.AddClassHandler((box, args) => box.OnSearchTextPropertyChanged(args)); + FilterModeProperty.Changed.AddClassHandler((box, args)=> box.OnFilterModePropertyChanged(args)); + ItemFilterProperty.Changed.AddClassHandler((box, args) => box.OnItemFilterPropertyChanged(args)); + ItemsSourceProperty.Changed.AddClassHandler((box, args) => box.OnItemsSourcePropertyChanged(args)); + ItemTemplateProperty.Changed.AddClassHandler((box, args) => box.OnItemTemplatePropertyChanged(args)); + IsEnabledProperty.Changed.AddClassHandler((box, args) => box.OnControlIsEnabledChanged(args)); } /// @@ -233,7 +236,7 @@ public partial class MultiAutoCompleteBox : TemplatedControl, IInnerContentContr private void OnTextBoxKeyDown(object sender, KeyEventArgs e) { - if ((e.Key == Key.Back || e.Key == Key.Delete )&& string.IsNullOrEmpty(TextBox?.Text) ) + if (e.Key is Key.Back or Key.Delete && string.IsNullOrEmpty(TextBox?.Text)) { if (SelectedItems?.Count > 0) { @@ -275,7 +278,7 @@ public partial class MultiAutoCompleteBox : TemplatedControl, IInnerContentContr /// use with AutoCompleteBox or deriving from AutoCompleteBox to /// create a custom control. /// - protected ISelectionAdapter? SelectionAdapter + private ISelectionAdapter? SelectionAdapter { get => _adapter; set @@ -566,9 +569,12 @@ public partial class MultiAutoCompleteBox : TemplatedControl, IInnerContentContr public const string PART_SelectedItemsControl = "PART_SelectedItemsControl"; private ItemsControl? _selectedItemsControl; + + /// protected override void OnLoaded(RoutedEventArgs e) { base.OnLoaded(e); + // TextBox can only be securely initialized after TextBox = (_selectedItemsControl?.ItemsPanelRoot as WrapPanelWithTrailingItem)?.TrailingItem as TextBox; } @@ -1614,11 +1620,11 @@ public partial class MultiAutoCompleteBox : TemplatedControl, IInnerContentContr /// Index function that retrieves the filter for the provided /// AutoCompleteFilterMode. /// - /// The built-in search mode. + /// The built-in search mode. /// Returns the string-based comparison function. - public static AutoCompleteFilterPredicate? GetFilter(AutoCompleteFilterMode FilterMode) + public static AutoCompleteFilterPredicate? GetFilter(AutoCompleteFilterMode filterMode) { - switch (FilterMode) + switch (filterMode) { case AutoCompleteFilterMode.Contains: return Contains;