Sunday, March 31, 2013

Windows Store App… How to set focus on a textbox

A common task in UI development is setting the focus on a textbox when navigating to a particular screen. When doing this on a Windows 8 application, a lot of developers try to do this inside the OnNavigatedTo event handler. This does not work. To set the focus on a textbox when the page is hit, call the focus method on the particular textbox inside the loaded event handler… That should do it.

<common:LayoutAwarePage
x:Name="pageRoot"

Loaded="pageRoot_Loaded_1">



private void pageRoot_Loaded_1(object sender, RoutedEventArgs e)
{
this.txtName.Focus(FocusState.Programmatic);
}



Hope this helps. Happy Coding!



No comments:

Post a Comment