diff --git a/src/Avalonia.Controls/DropDown.cs b/src/Avalonia.Controls/DropDown.cs index 5349fb1ca7..fa2e0c1e16 100644 --- a/src/Avalonia.Controls/DropDown.cs +++ b/src/Avalonia.Controls/DropDown.cs @@ -120,21 +120,21 @@ namespace Avalonia.Controls /// 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); } diff --git a/tests/Avalonia.Controls.UnitTests/DropDownTests.cs b/tests/Avalonia.Controls.UnitTests/DropDownTests.cs index 30cca90b4a..b5de8c67fa 100644 --- a/tests/Avalonia.Controls.UnitTests/DropDownTests.cs +++ b/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() {