How to navigate between controls on KeyDown event.

David Simões 21 Reputation points
2021-08-19T15:57:34.337+00:00

I'm trying to have a KeyDown function that makes the focus move to the next control.

I've tried this but it throws me an error:

private void Obj_KeyDown(object sender, KeyRoutedEventArgs e)
    {
        if (e.Key.ToString() == "Enter")  FocusManager.TryMoveFocus(FocusNavigationDirection.Down);
    }

Error:

System.Runtime.InteropServices.COMException: 'Catastrophic failure (0x8000FFFF (E_UNEXPECTED))'

Any hint on how can I make this work?

Thanks,
David

Universal Windows Platform (UWP)
Windows App SDK
Windows App SDK
A set of Microsoft open-source libraries, frameworks, components, and tools to be used in apps to access Windows platform functionality on many versions of Windows. Previously known as Project Reunion.
745 questions
C#
C#
An object-oriented and type-safe programming language that has its roots in the C family of languages and includes support for component-oriented programming.
10,562 questions
0 comments No comments
{count} votes

Accepted answer
  1. Viorel 114K Reputation points
    2021-08-19T17:41:32.727+00:00

    Check if a workaround works for you (https://github.com/microsoft/microsoft-ui-xaml/issues/5593):

    . . .
    var fneo = new FindNextElementOptions { SearchRoot = tb.XamlRoot.Content };
    FocusManager.TryMoveFocus(FocusNavigationDirection.Next, fneo);
    . . .
    

    Use an appropriate control for tb.

    1 person found this answer helpful.

0 additional answers

Sort by: Most helpful