Your Privacy Matters: We use our own and third-party cookies to improve your experience on our website. By continuing to use the website we understand that you accept their use. Cookie Policy
285
Keyboard Selection Event of ComboBox Items
posted

How I can get Selected Item when I navigate trough the List via Keyboard?

It looks like MHE is selected but no Property of the XamComboEditor is filled with that Item

So in my Usecase I registered the PreviewKeyDown Event, so when I hit Enter I want to have the Selected Item which is selected by Keyboard.

The Problem is that in the PreviewKeyDown Event it's not possible for me to find out which item was now selected via KeyUp and KeyDown.

I have to know it in PreviewKeyDown because I have to work with that item in that Event Handler. Is there a chance to get it? So The SelectionChangedEvent is fired after KeyDown and then its too late for me (so the SelectedItem is set also then)

CustomValueEnteredAction = CustomValueEnteredActions.Allow;

Parents
No Data
Reply
  • 6365
    Verified Answer
    Offline posted

    Hello CSchmitt,

    Thank you for the screenshots you have provided.

    The blue highlight rectangle around the ComboEditorItemControl from your first image indicates the currently active/focused element (the one that will get selected if Enter is pressed) and not the selected one.

    In order to get the underlying data item of the currently active combo item inside the PreviewKeyDown event of the XamComboEditor prior to the SelectionChanged event, I can suggest you go down through the visual tree of the editor's dropdown and get the respective ComboEditorItemControl whose focus rectangle is currently visible.

    Code-behind:

    private void combo_PreviewKeyDown(object sender, KeyEventArgs e)
    {
        var combo = (sender as XamComboEditor);
               
        if (combo.IsDropDownOpen && e.Key == Key.Enter)
        {
            var item = GetActiveItem(combo);
            MessageBox.Show("Active item: " + item);
        }
    }

    private static object GetActiveItem(XamComboEditor combo)
    {
        var dropdownPopup = GetOpenPopups().FirstOrDefault().Child;
        if (dropdownPopup == null)
            return null;

        var itemsPanel = Utilities.GetDescendantFromName(dropdownPopup, "ItemsPanel") as ItemsPanel;
        if (itemsPanel == null)
            return null;

        // Get the ComboEditorItemControl whose inner focus rectangle is visible.
        // (the blue highlight around the item indicates it is visible)
        //
        var activeComboEditorItemControl = itemsPanel.Children
                                                .OfType<ComboEditorItemControl>()
                                                .FirstOrDefault(it => (Utilities.GetDescendantFromName(it, "FocusVisualElement") as Rectangle).IsVisible);
               
        var item = activeComboEditorItemControl.Item.Data;
        return item;
    }

    I have attached a sample application that demonstrates the approach from above.

    If you have any questions, please let me know.

    sharedXamComboEditor_sample.zip
Children