Setting focus in WPF

posted in: Uncategorized | 0


Setting focus to a particular element on a screen seems like a simple and common task.  It gives the user that nice fuzzy feeling when they can just start typing in the first field of a screen without having to click into it with their mouse.  In WPF it doesn’t seem that easy to do.  For a lot of screens I was able to do this in XAML using the Focus Manager e.g.

FocusManager.FocusedElement="{Binding ElementName=firstNameBox}" 

This worked pretty well in 90% of cases until I tried using this in an application using CAL where the controls are reused.  In this case I ended up having to use codebehind to set the focus on visibility changed and use element.Focus(). Seemed to be the only way to do this so it would set the correct visible field every time.  Anyone else got a better way?


Technorati Tags: ,