Sfoglia il codice sorgente

Enable Copy and Cut in ContextMenu for AutoCompleteBox (#19087)

Co-authored-by: Julien Lebosquain <[email protected]>
yoyo 2 mesi fa
parent
commit
06d003b4a8

+ 19 - 4
src/Avalonia.Controls/AutoCompleteBox/AutoCompleteBox.Properties.cs

@@ -22,7 +22,7 @@ namespace Avalonia.Controls
         public static readonly StyledProperty<int> CaretIndexProperty =
             TextBox.CaretIndexProperty.AddOwner<AutoCompleteBox>(new(
                 defaultValue: 0,
-                defaultBindingMode:BindingMode.TwoWay));
+                defaultBindingMode: BindingMode.TwoWay));
 
         public static readonly StyledProperty<string?> WatermarkProperty =
             TextBox.WatermarkProperty.AddOwner<AutoCompleteBox>();
@@ -72,6 +72,12 @@ namespace Avalonia.Controls
             AvaloniaProperty.Register<AutoCompleteBox, IDataTemplate>(
                 nameof(ItemTemplate));
 
+        /// <summary>
+        /// Defines the <see cref="ClearSelectionOnLostFocus"/> property
+        /// </summary>
+        public static readonly StyledProperty<bool> ClearSelectionOnLostFocusProperty =
+            TextBox.ClearSelectionOnLostFocusProperty.AddOwner<AutoCompleteBox>();
+
         /// <summary>
         /// Identifies the <see cref="IsDropDownOpen" /> property.
         /// </summary>
@@ -295,6 +301,15 @@ namespace Avalonia.Controls
             set => SetValue(IsDropDownOpenProperty, value);
         }
 
+        /// <summary>
+        /// Gets or sets a value that determines whether the <see cref="AutoCompleteBox"/> clears its selection after it loses focus.
+        /// </summary>
+        public bool ClearSelectionOnLostFocus
+        {
+            get => GetValue(ClearSelectionOnLostFocusProperty);
+            set => SetValue(ClearSelectionOnLostFocusProperty, value);
+        }
+
         /// <summary>
         /// Gets or sets the  <see cref="T:Avalonia.Data.Binding" /> 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);
         }
-       
+
         /// <summary>
         /// Gets or sets the maximum number of characters that the <see cref="AutoCompleteBox"/> 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);
         }
-      
+
         /// <summary>
         /// Gets or sets custom content that is positioned on the left side of the text layout box
         /// </summary>
@@ -511,6 +526,6 @@ namespace Avalonia.Controls
         {
             get => GetValue(InnerRightContentProperty);
             set => SetValue(InnerRightContentProperty, value);
-        }      
+        }
     }
 }

+ 4 - 1
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();
                 }

+ 1 - 0
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)}"

+ 1 - 0
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}"