Skip to content

Commit fb52456

Browse files
committed
feat: Support Enter, Space and Escape on ComboBox
1 parent 41d1bdc commit fb52456

File tree

2 files changed

+57
-14
lines changed

2 files changed

+57
-14
lines changed

src/Uno.UI/UI/Xaml/Controls/ComboBox/ComboBox.cs

Lines changed: 30 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,29 +1,18 @@
11
#nullable enable
22

33
using System;
4-
using System.Collections.Generic;
5-
using System.Text;
6-
using System.Windows.Input;
7-
using Uno.Client;
8-
using System.Collections;
9-
using Uno.UI.Controls;
10-
using Uno.Extensions;
114
using Windows.UI.Xaml.Automation.Peers;
125
using Windows.UI.Xaml.Input;
136
using Uno.Foundation.Logging;
147
using Windows.UI.Xaml.Media;
15-
using Windows.UI.Xaml;
168
using Windows.UI.Xaml.Controls.Primitives;
179
using Windows.Foundation;
1810
using Uno.UI;
19-
using System.Linq;
20-
using Windows.UI.ViewManagement;
2111
using Windows.UI.Xaml.Data;
2212

2313

2414
using Uno.UI.DataBinding;
2515
using Uno.UI.Xaml.Controls;
26-
using Windows.UI.Core;
2716
#if __ANDROID__
2817
using Android.Views;
2918
using _View = Android.Views.View;
@@ -35,6 +24,7 @@
3524
using _View = AppKit.NSView;
3625
#else
3726
using _View = Windows.UI.Xaml.FrameworkElement;
27+
using Windows.System;
3828
#endif
3929

4030
#if HAS_UNO_WINUI
@@ -490,6 +480,35 @@ protected override void OnPointerReleased(PointerRoutedEventArgs args)
490480
args.Handled = true;
491481
}
492482

483+
protected override void OnKeyDown(KeyRoutedEventArgs args)
484+
{
485+
args.Handled = TryHandleKeyDown(args);
486+
487+
base.OnKeyDown(args);
488+
}
489+
490+
internal bool TryHandleKeyDown(KeyRoutedEventArgs args)
491+
{
492+
if (args.Key == VirtualKey.Enter ||
493+
args.Key == VirtualKey.Space)
494+
{
495+
if (!IsDropDownOpen)
496+
{
497+
IsDropDownOpen = true;
498+
return true;
499+
}
500+
}
501+
else if (args.Key == VirtualKey.Escape)
502+
{
503+
if (IsDropDownOpen)
504+
{
505+
IsDropDownOpen = false;
506+
return true;
507+
}
508+
}
509+
return false;
510+
}
511+
493512
/// <summary>
494513
/// Stretches the opened Popup horizontally, and uses the VerticalAlignment
495514
/// of the first child for positioning.
Lines changed: 27 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
1-
using System;
2-
using System.Collections.Generic;
3-
using System.Text;
1+
using Windows.System;
42
using Windows.UI.Xaml.Controls.Primitives;
3+
using Windows.UI.Xaml.Input;
54

65
namespace Windows.UI.Xaml.Controls
76
{
@@ -11,5 +10,30 @@ public ComboBoxItem()
1110
{
1211
DefaultStyleKey = typeof(ComboBoxItem);
1312
}
13+
14+
protected override void OnKeyDown(KeyRoutedEventArgs args)
15+
{
16+
if (ItemsControl.ItemsControlFromItemContainer(this) is ComboBox comboBox)
17+
{
18+
if (args.Key == VirtualKey.Enter && comboBox.IsDropDownOpen)
19+
{
20+
var item = comboBox.ItemFromContainer(this);
21+
if (item != null)
22+
{
23+
comboBox.SelectedItem = item;
24+
comboBox.IsDropDownOpen = false;
25+
args.Handled = true;
26+
}
27+
}
28+
29+
if (!args.Handled)
30+
{
31+
// Fallback to combobox keydown handling
32+
args.Handled = comboBox.TryHandleKeyDown(args);
33+
}
34+
}
35+
36+
base.OnKeyDown(args);
37+
}
1438
}
1539
}

0 commit comments

Comments
 (0)