WPF Focus – Different on Win XP than Win 7

Focus issues are notoriously challenging. I just ran into a case where it even acts differently on Win XP vs. Win 7.

I have an textbox that I re-templated to use the Auto Complete from the WPF Toolkit. The goal was to turn a normal textbox into an autocomplete by simply setting a style. So, in this case, I override the TextBox control template and embed an autocomplete inside of it.

… But now when I tab through the controls, it stops once for the parent textbox and then again for the contained autocomplete inside.

Fix #1 – Make IsTabStop=”False” for topmost TextBox

This worked, and I thought life was good. That is until I ran it on XP. It works fine on Win 7, but on XP the focus logic completely skips over the control. Not good.

I tried various combinations of IsTabStop and Focusable settings for the parent / child. No luck.

Fix #2 – Add A Behavior that “forwards” focus

When all else fails, drop into code, right? So I coded up a “Forward Focus” behavior. Basically, if I get focus on the parent TextBox, then just set focus to the contained AutoComplete. Works like a charm… in one navigation direction. Tab, Tab, Tab bumps it right through as expected on Win7 and WinXP!

… But what about Shift+Tab? That should take me back up the tree. Unfortunately it gets stuck inside the box because as soon as the focus moves to the parent TextBox, ForwardFocus moves focus back down the tree. Ugh!

I had to try a couple strategies to get it working, but I eventually settled on some logic that worked by sending focus back up the tree if the user is using the Tab key.


Managing focus is a PITA, but is something that we have to do. However, I was a bit shocked by this experience to see that XP would handle this differently than Win7.


Here’s the code that I settled on for the “ForwardFocusBehavior”. Nothing crazy, but took some fiddling to get it right.



Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

Create a website or blog at WordPress.com

Up ↑

%d bloggers like this: