wpfxamlmvvmprism-4

Set Focus OnNavigatedTo in Prism for WPF Application


I would like on navigation between views in a WPF application using Prism to have the ability to set focus to sepecific textboxes so a user can perform navigation and then begin typing in the relevant textbox without a second click into the textbox.

I have an application built with Prism that has a Shell with a ContentControl "MainContentRegionContentControl". I then have some buttons across the top when on clicking them I do a region.RequestNavigate("UserControlToLoad"). On the UserControl I have the OnNavigatedTo and in that method I call this.MainTextBox.Focus().

The above doesn't appear to work, the navigation appears to work and the OnNavigatedTo method is called, but the textbox doesn't have focus.

I've added FocusManager.IsFocusScope="True" to the textbox, but this hasn't made a difference.


Solution

  • Use Loaded() method for each page instead of OnNavigatedTo() and TextBox.Focus() will work fine then ;)