Repository: AvaloniaUI/Avalonia

Issue:
AutoCompleteBox loses selection when context menu opened
### Describe the bug

Selection of text get lost when context menu opened.
Moreover, small part of selection (not empty) is expanded to full selection at first time and lost too.


### To Reproduce

1. Write text in AutoCompleteBox
2. Select part of text (or not)
3. Right click on selection
4. Move pointer over context menu items
5. Cut, Copy items after 4. will be disabled with selection empty


Example styles:
```xml
<Styles.Resources>
...
<MenuFlyout x:Key="testTextBoxContextFlyout" Placement="Bottom">
 <MenuItem Header="Cut" Command="{Binding $parent[TextBox].Cut}" IsEnabled="{Binding $parent[TextBox].CanCut}" InputGesture="{x:Static TextBox.CutGesture}"/>
 <MenuItem Header="Copy" Command="{Binding $parent[TextBox].Copy}" IsEnabled="{Binding $parent[TextBox].CanCopy}" InputGesture="{x:Static TextBox.CopyGesture}"/>
 <MenuItem Header="Paste" Command="{Binding $parent[TextBox].Paste}" IsEnabled="{Binding $parent[TextBox].CanPaste}" InputGesture="{x:Static TextBox.PasteGesture}"/>
</MenuFlyout>
...
</Styles.Resources>

<Style Selector="TextBox">
 <Setter Property="ContextFlyout" Value="{StaticResource testTextBoxContextFlyout}"/>
</Style>
```


### Expected behavior

Not lose selection.
Not expand not empty selection.

### Avalonia version

11.2.0

### OS

Windows, macOS, Linux

### Additional context

The same problem with TextBox was solved:
https://github.com/AvaloniaUI/Avalonia/pull/4498



Hints:
@isergvb if you want to apply a similar fix to that control, a PR is welcome. 

Patch:
diff --git a/src/Avalonia.Controls/AutoCompleteBox/AutoCompleteBox.cs b/src/Avalonia.Controls/AutoCompleteBox/AutoCompleteBox.cs
index 95ea8f9ece2..b142ce5eb2d 100644
--- a/src/Avalonia.Controls/AutoCompleteBox/AutoCompleteBox.cs
+++ b/src/Avalonia.Controls/AutoCompleteBox/AutoCompleteBox.cs
@@ -812,7 +812,11 @@ private void FocusChanged(bool hasFocus)
                 }
 
                 _userCalledPopulate = false;
-                ClearTextBoxSelection();
+
+                if (ContextMenu is not { IsOpen: true })
+                {
+                    ClearTextBoxSelection();
+                }
             }
 
             _isFocused = hasFocus;


Test patch:
diff --git a/tests/Avalonia.Controls.UnitTests/AutoCompleteBoxTests.cs b/tests/Avalonia.Controls.UnitTests/AutoCompleteBoxTests.cs
index 5cc3901cd74..67f55055ec8 100644
--- a/tests/Avalonia.Controls.UnitTests/AutoCompleteBoxTests.cs
+++ b/tests/Avalonia.Controls.UnitTests/AutoCompleteBoxTests.cs
@@ -1,4 +1,4 @@
-﻿using System;
+using System;
 using System.Collections.Generic;
 using System.Linq;
 using Avalonia.Controls.Primitives;
@@ -9,7 +9,10 @@
 using Xunit;
 using System.Collections.ObjectModel;
 using System.Reactive.Subjects;
+using Avalonia.Headless;
 using Avalonia.Input;
+using Avalonia.Platform;
+using Moq;
 
 namespace Avalonia.Controls.UnitTests
 {
@@ -520,6 +523,42 @@ public void Attempting_To_Open_Without_Items_Does_Not_Prevent_Future_Opening_Wit
             });
         }
 
+        [Fact]
+        public void Opening_Context_Menu_Does_not_Lose_Selection()
+        {
+            using (UnitTestApplication.Start(FocusServices))
+            {
+                var target1 = CreateControl();
+                target1.ContextMenu = new TestContextMenu();
+                var textBox1 = GetTextBox(target1);
+                textBox1.Text = "1234";
+
+                var target2 = CreateControl();
+                var textBox2 = GetTextBox(target2);
+                textBox2.Text = "5678";
+
+                var sp = new StackPanel();
+                sp.Children.Add(target1);
+                sp.Children.Add(target2);
+
+                target1.ApplyTemplate();
+                target2.ApplyTemplate();
+
+                var root = new TestRoot() { Child = sp };
+
+                textBox1.SelectionStart = 0;
+                textBox1.SelectionEnd = 3;
+
+                target1.Focus();
+                Assert.False(target2.IsFocused);
+                Assert.True(target1.IsFocused);
+
+                target2.Focus();
+
+                Assert.Equal("123", textBox1.SelectedText);
+            }
+        }
+
         /// <summary>
         /// Retrieves a defined predicate filter through a new AutoCompleteBox 
         /// control instance.
@@ -1198,5 +1237,22 @@ private IControlTemplate CreateTemplate()
                 return panel;
             });
         }
+
+        private static TestServices FocusServices => TestServices.MockThreadingInterface.With(
+            focusManager: new FocusManager(),
+            keyboardDevice: () => new KeyboardDevice(),
+            keyboardNavigation: () => new KeyboardNavigationHandler(),
+            inputManager: new InputManager(),
+            standardCursorFactory: Mock.Of<ICursorFactory>(),
+            textShaperImpl: new HeadlessTextShaperStub(),
+            fontManagerImpl: new HeadlessFontManagerStub());
+
+        private class TestContextMenu : ContextMenu
+        {
+            public TestContextMenu()
+            {
+                IsOpen = true;
+            }
+        }
     }
 }