From 06d003b4a84210475fa76b79cc3f2d475d7f3a13 Mon Sep 17 00:00:00 2001 From: yoyo <143886016+zrt2399@users.noreply.github.com> Date: Tue, 12 Aug 2025 17:49:13 +0800 Subject: [PATCH] Enable Copy and Cut in ContextMenu for AutoCompleteBox (#19087) Co-authored-by: Julien Lebosquain --- .../AutoCompleteBox.Properties.cs | 23 +++++++++++++++---- .../AutoCompleteBox/AutoCompleteBox.cs | 5 +++- .../Controls/AutoCompleteBox.xaml | 1 + .../Controls/AutoCompleteBox.xaml | 1 + 4 files changed, 25 insertions(+), 5 deletions(-) diff --git a/src/Avalonia.Controls/AutoCompleteBox/AutoCompleteBox.Properties.cs b/src/Avalonia.Controls/AutoCompleteBox/AutoCompleteBox.Properties.cs index 70be01e316..a667087c37 100644 --- a/src/Avalonia.Controls/AutoCompleteBox/AutoCompleteBox.Properties.cs +++ b/src/Avalonia.Controls/AutoCompleteBox/AutoCompleteBox.Properties.cs @@ -22,7 +22,7 @@ namespace Avalonia.Controls public static readonly StyledProperty CaretIndexProperty = TextBox.CaretIndexProperty.AddOwner(new( defaultValue: 0, - defaultBindingMode:BindingMode.TwoWay)); + defaultBindingMode: BindingMode.TwoWay)); public static readonly StyledProperty WatermarkProperty = TextBox.WatermarkProperty.AddOwner(); @@ -72,6 +72,12 @@ namespace Avalonia.Controls AvaloniaProperty.Register( nameof(ItemTemplate)); + /// + /// Defines the property + /// + public static readonly StyledProperty ClearSelectionOnLostFocusProperty = + TextBox.ClearSelectionOnLostFocusProperty.AddOwner(); + /// /// Identifies the property. /// @@ -295,6 +301,15 @@ namespace Avalonia.Controls set => SetValue(IsDropDownOpenProperty, value); } + /// + /// Gets or sets a value that determines whether the clears its selection after it loses focus. + /// + public bool ClearSelectionOnLostFocus + { + get => GetValue(ClearSelectionOnLostFocusProperty); + set => SetValue(ClearSelectionOnLostFocusProperty, value); + } + /// /// Gets or sets the that /// is used to get the values for display in the text portion of @@ -484,7 +499,7 @@ namespace Avalonia.Controls get => GetValue(ItemsSourceProperty); set => SetValue(ItemsSourceProperty, value); } - + /// /// Gets or sets the maximum number of characters that the can accept. /// This constraint only applies for manually entered (user-inputted) text. @@ -494,7 +509,7 @@ namespace Avalonia.Controls get => GetValue(MaxLengthProperty); set => SetValue(MaxLengthProperty, value); } - + /// /// Gets or sets custom content that is positioned on the left side of the text layout box /// @@ -511,6 +526,6 @@ namespace Avalonia.Controls { get => GetValue(InnerRightContentProperty); set => SetValue(InnerRightContentProperty, value); - } + } } } diff --git a/src/Avalonia.Controls/AutoCompleteBox/AutoCompleteBox.cs b/src/Avalonia.Controls/AutoCompleteBox/AutoCompleteBox.cs index 4241f6315f..f61f6ff2a1 100644 --- a/src/Avalonia.Controls/AutoCompleteBox/AutoCompleteBox.cs +++ b/src/Avalonia.Controls/AutoCompleteBox/AutoCompleteBox.cs @@ -813,7 +813,10 @@ namespace Avalonia.Controls _userCalledPopulate = false; - if (ContextMenu is not { IsOpen: true }) + var textBoxContextMenuIsOpen = TextBox?.ContextFlyout?.IsOpen == true || TextBox?.ContextMenu?.IsOpen == true; + var contextMenuIsOpen = ContextFlyout?.IsOpen == true || ContextMenu?.IsOpen == true; + + if (!textBoxContextMenuIsOpen && !contextMenuIsOpen && ClearSelectionOnLostFocus) { ClearTextBoxSelection(); } diff --git a/src/Avalonia.Themes.Fluent/Controls/AutoCompleteBox.xaml b/src/Avalonia.Themes.Fluent/Controls/AutoCompleteBox.xaml index 3d9db1de3b..3196d262a1 100644 --- a/src/Avalonia.Themes.Fluent/Controls/AutoCompleteBox.xaml +++ b/src/Avalonia.Themes.Fluent/Controls/AutoCompleteBox.xaml @@ -42,6 +42,7 @@ BorderThickness="{TemplateBinding BorderThickness}" CornerRadius="{TemplateBinding CornerRadius}" CaretIndex="{TemplateBinding CaretIndex, Mode=TwoWay}" + ClearSelectionOnLostFocus="{TemplateBinding ClearSelectionOnLostFocus}" Padding="{TemplateBinding Padding}" Margin="0" DataValidationErrors.Errors="{TemplateBinding (DataValidationErrors.Errors)}" diff --git a/src/Avalonia.Themes.Simple/Controls/AutoCompleteBox.xaml b/src/Avalonia.Themes.Simple/Controls/AutoCompleteBox.xaml index 23b756cdc9..10b48a12f6 100644 --- a/src/Avalonia.Themes.Simple/Controls/AutoCompleteBox.xaml +++ b/src/Avalonia.Themes.Simple/Controls/AutoCompleteBox.xaml @@ -17,6 +17,7 @@ BorderThickness="{TemplateBinding BorderThickness}" CornerRadius="{TemplateBinding CornerRadius}" CaretIndex="{TemplateBinding CaretIndex, Mode=TwoWay}" + ClearSelectionOnLostFocus="{TemplateBinding ClearSelectionOnLostFocus}" DataValidationErrors.Errors="{TemplateBinding (DataValidationErrors.Errors)}" Watermark="{TemplateBinding Watermark}" MaxLength="{TemplateBinding MaxLength}"