Search code examples
c#wpfcomboboxfocus

WPF and C# ComboBox Next Focus


Im using WPF and C#. I have an application with an OnScreenKeyboard made with buttons. When I press the tab button in the OnScreenKeyboard it does this:

if (IsEnterEnabled){
  CurrentTextBox.MoveFocus(new TraversalRequest(FocusNavigationDirection.Next));
}

CurrentTextBox is a Control.

When a textbox is focused it works fine and move to the next focus, even if the next focus is a comboBox, but if the current focus is in an editable combo box it doesnt move to next focus element.

If the combo box is not editable, it works.


Solution

  • Since the ComboBox has a TextBox inside it which currently has focus, telling ComboBox to move next will only move to the TextBox. Instead, get the inner TextBox and move next on that:

            var s1 = FocusManager.GetFocusedElement(this);
            if (s1 is FrameworkElement)
            {
                ((FrameworkElement)s1).MoveFocus(new TraversalRequest(FocusNavigationDirection.Next));
            }