Goal: issue an event when items in a combobox drop down list is selected.
Problem: Using \"SelectionChanged\", however, if the user
For UWP, I tried a different approach. I extended the ComboBox class, and processed the SelectionChanged and OnKeyUp events on the ComboBox as well as the Tapped event on the ComboBoxItems. In cases where I get a Tapped event or an Enter or Space key without first getting a SelectionChanged then I know the current item has been re-selected and I respond accordingly.
class ExtendedComboBox : ComboBox
{
public ExtendedComboBox()
{
SelectionChanged += OnSelectionChanged;
}
protected override void PrepareContainerForItemOverride(Windows.UI.Xaml.DependencyObject element, object item)
{
ComboBoxItem cItem = element as ComboBoxItem;
if (cItem != null)
{
cItem.Tapped += OnItemTapped;
}
base.PrepareContainerForItemOverride(element, item);
}
protected override void OnKeyUp(KeyRoutedEventArgs e)
{
// if the user hits the Enter or Space to select an item, then consider this a "reselect" operation
if ((e.Key == Windows.System.VirtualKey.Space || e.Key == Windows.System.VirtualKey.Enter) && !isSelectionChanged)
{
// handle re-select logic here
}
isSelectionChanged = false;
base.OnKeyUp(e);
}
// track whether or not the ComboBox has received a SelectionChanged notification
// in cases where it has not yet we get a Tapped or KeyUp notification we will want to consider that a "re-select"
bool isSelectionChanged = false;
private void OnSelectionChanged(object sender, SelectionChangedEventArgs e)
{
isSelectionChanged = true;
}
private void OnItemTapped(object sender, TappedRoutedEventArgs e)
{
if (!isSelectionChanged)
{
// indicates that an item was re-selected - handle logic here
}
isSelectionChanged = false;
}
}