WPF Controls TabIndex: Moving to the First Input Control

Posted Thursday, October 25, 2007 1:07 AM by cromwellryan

Here's one of those real-world situations that no one bothers to demo.  If you are building a form or have some sort of input your gathering from the user, you'll likely want the first input control Focused initially.  You have a few ways to do this (in ascending order of recommendation):

  1. Call Focus() on the first control explicitly (meh).
  2. Iterate the controls collection and find the lowest TabIndex (terrible code smell).
  3. Do the following in the Load event:
this.MoveFocus(new TraversalRequest(FocusNavigationDirection.Next));
Filed under: ,

Comments

# re: WPF Controls TabIndex: Moving to the First Input Control

Wednesday, January 02, 2008 8:32 AM by Rob

Use this attached property on the window: FocusManager.FocusedElement="{Binding ElementName=yourFocusedControlName}"

# re: WPF Controls TabIndex: Moving to the First Input Control

Wednesday, January 02, 2008 8:39 AM by cromwellryan

Very nice.  Much more clean.  Thanks.

Leave a Comment

(required) 
(required) 
(optional)
(required)