views:

170

answers:

1

I want to navigate in my window with the arrow key. It works so far but if I reach the end of my window, focus wraps to the first main menu item. But I want that focus stops at the last control in my window.

private void Window_PreviewKeyDown(object sender, KeyEventArgs e)
{
   if (e.Key == Key.Down)
   {
     elem.MoveFocus(FocusNavigationDirection.Next);
   }
}

"elem" is indirect child control of "stackPanel". MoveFocus always returns true and I already tried: KeyboardNavigation.SetTabNavigation(stackPanel, KeyboardNavigationMode.Contained); KeyboardNavigation.SetDirectionalNavigation(stackPanel,KeyboardNavigationMode.Contained); KeyboardNavigation.SetControlTabNavigation(stackPanel, KeyboardNavigationMode.Contained);

Nothing helped.

A: 

How about using a TraversalRequest instance?

if (e.Key == Key.Down)
{
    e.Handled = true;
    elem.MoveFocus(new TraversalRequest 
    { 
        FocusNavigationDirection = FocusNavigationDirection.Next,
        Wrapped = false
    });
}

Mind you, the MSDN documentation for TraversalRequest.Wrapped says that it's false by default, so the focus shouldn't wrap anyway.

Matt Hamilton
I now tried this but it didn't not work. After traversing all my controls it continues with the mainmenu items from my mainwindow.
codymanix