Thanks to an answer on a previous question (Previous Question), I now have a body of code that navigates WPF tab stops (shown below). It works fine except for the first tab stop. Calling this.MoveFocus(...First) and followed by FocusManager.GetFocusedElement returns null. Any ideas? How do I get the first tab stop in my window?
Thanks, - Mike
// Select the first element in the window
this.MoveFocus(new TraversalRequest(FocusNavigationDirection.First));
TraversalRequest next = new TraversalRequest(FocusNavigationDirection.Next);
List<IInputElement> elements = new List<IInputElement>();
// Get the current element.
UIElement currentElement = FocusManager.GetFocusedElement(this) as UIElement;
while (currentElement != null)
{
elements.Add(currentElement);
// Get the next element.
currentElement.MoveFocus(next);
currentElement = FocusManager.GetFocusedElement(this) as UIElement;
// If we looped (If that is possible), exit.
if (elements[0] == currentElement)
break;
}
I needed to do something similar in a project I'm working on, and I found something that seems to work well enough.
Here's a quick demo project with the code:
XAML:
Code Behind:
I know this is really late response, but does this help you at all?