I’ve got a WPF browser-like application with a few pages. When I switch between pages, I’d like to set the keyboard focus.
When a page is loaded the first time, this works by calling Control.Focus() in the constructor.
But when I switch between pages this does not work anymore – the focus is just on the first field, and ignores my attempts to change it to anything else 🙁
The pages have the attribute KeepAlive=true – it would be OK if that would keep the focus alive, too, but just setting the focus to the first field is annoying.
I tried to set the focus in the loaded event, but it did not work, too. It seems the default focus is set after reloading the page.
Is there any way to set the focus on entering a page the second time? When, how and where should I set the focus when switching between WPF pages in a browserlike application?
Since I found no solution to this problem, I used a simple workaround:
I fire up a secondary thread, which changes the focus after the page has loaded.
Luckily this is done very easily using BeginInvoke:
This worked way better than I ever expected it to, so probably this workaround will stay in use for a long time.