Tuesday, March 11, 2008

Canceling keyboard focus change in WPF

Hah! Today I found out that the keyboard focus transition in WPF  is cancellable :)
It turned out that you should "handle" PreviewLostKeyboardFocus routed event (just set it's Handled to True) and magic happens - focus does not go anywhere.
Still, there are some cases when this event is not raised, for example, when user just Alt-Tabs to another application, but that does not matter in most cases :)

2 comments:

Anonymous said...

Nothing to do with focus event, really, but I couldn't help but find your pic funny! ;-)

Ahum, sorry, yeah that's cool to cancel focus change event, but, ahem.... I could see headaches on the horizon...

Rat's Blog said...

:)

I know - canceling focus is really not a good option, and I personally hate such kind of behavior, but sometimes you have to do some terrible things to satisfy the client :):)