Browse Source

Fix DropDown popup opening.

When the `DropDown` control is clicked, toggle the opening of the popup.

Fixes #1169.
Steven Kirk 8 years ago
parent
commit
268587c432
2 changed files with 34 additions and 12 deletions
  1. 10 10
      src/Avalonia.Controls/DropDown.cs
  2. 24 2
      tests/Avalonia.Controls.UnitTests/DropDownTests.cs

+ 10 - 10
src/Avalonia.Controls/DropDown.cs

@@ -120,21 +120,21 @@ namespace Avalonia.Controls
         /// <inheritdoc/>
         protected override void OnPointerPressed(PointerPressedEventArgs e)
         {
-            if (!IsDropDownOpen && ((IVisual)e.Source).GetVisualRoot() is PopupRoot)
-            {
-                IsDropDownOpen = true;
-                e.Handled = true;
-            }
-
             if (!e.Handled)
             {
-                if (UpdateSelectionFromEventSource(e.Source))
+                if (((IVisual)e.Source).GetVisualRoot() is PopupRoot)
                 {
-                    _popup?.Close();
-                    e.Handled = true;
+                    if (UpdateSelectionFromEventSource(e.Source))
+                    {
+                        _popup?.Close();
+                        e.Handled = true;
+                    }
+                }
+                else
+                {
+                    IsDropDownOpen = !IsDropDownOpen;
                 }
             }
-
             base.OnPointerPressed(e);
         }
 

+ 24 - 2
tests/Avalonia.Controls.UnitTests/DropDownTests.cs

@@ -1,21 +1,43 @@
 // Copyright (c) The Avalonia Project. All rights reserved.
 // Licensed under the MIT license. See licence.md file in the project root for full license information.
 
-using System.Linq;
 using Avalonia.Controls.Presenters;
 using Avalonia.Controls.Primitives;
 using Avalonia.Controls.Shapes;
 using Avalonia.Controls.Templates;
+using Avalonia.Input;
 using Avalonia.LogicalTree;
 using Avalonia.Media;
 using Avalonia.UnitTests;
-using Avalonia.VisualTree;
 using Xunit;
 
 namespace Avalonia.Controls.UnitTests
 {
     public class DropDownTests
     {
+        [Fact]
+        public void Clicking_On_Control_Toggles_IsDropDownOpen()
+        {
+            var target = new DropDown
+            {
+                Items = new[] { "Foo", "Bar" },
+            };
+
+            target.RaiseEvent(new PointerPressedEventArgs
+            {
+                RoutedEvent = InputElement.PointerPressedEvent,
+            });
+
+            Assert.True(target.IsDropDownOpen);
+
+            target.RaiseEvent(new PointerPressedEventArgs
+            {
+                RoutedEvent = InputElement.PointerPressedEvent,
+            });
+
+            Assert.False(target.IsDropDownOpen);
+        }
+
         [Fact]
         public void SelectionBoxItem_Is_Rectangle_With_VisualBrush_When_Selection_Is_Control()
         {