Focus In An ItemsControl
I have an ItemsControl that uses a DataTemplate. The DataTempl开发者_开发百科ate contains a TextBox, which can receive keyboard focus. I need to be able to move the keyboard focus from the currently focused TextBox in the DataTemplate to the next TextBox, as if the Tab key has been pressed. I've noticed that there is a UIElement.MoveFocus() method, but this begs the question as to which UIElement should be used to call the method. This is probably the reason why I haven't gotten this method to work for me... Any help would be really appreciated!
Thanks,
Andrew
You should be able to call the MoveFocus method on your Window (or Page, depending on what your top-level container is).
this.MoveFocus(new TraversalRequest(FocusNavigationDirection.Next));
That'll tell WPF to move the focus to the next control, either using the TabIndex properties on the controls, or just moving across and down naturally to find the next logical control.
Failing that you could call MoveFocus on your ItemsControl directly (give it a name and replace the "this" with that name in the code above).
精彩评论