codymanix
codymanix

Reputation: 29490

Global PreviewKeyDown handler vs local PreviewKeyDown handler

I have a PreviewKeyDown handler on my mainwindow which handles up and down keys so I can navigate with the keyboard between my controls.

Now I have the problem that in some Textboxes I also want to use the up/down keys. This seems impossible because the other handler seems to swallow the keys first.

Is it possible that when one of these TextBox controls are focused they get the up/down keys first and then then swallow them so that the "global" PreviewKeyDown does not get them?

Sure I could disable the global handler somehow when such a TextBox got focus but is this good style?

Upvotes: 0

Views: 1488

Answers (1)

Andy
Andy

Reputation: 30418

You don't really have an option, aside from filtering out those keys in the global key handler.

The reason that you're having this problem is that all of the Preview* events are tunneling, meaning that controls higher in the visual tree get them first (starting at the root). The very reason why you're using this event in the first place is causing your problem.

One less than ideal option would be to register a class handler for TextBox.PreviewKeyDown (see EventManager.RegisterClassHandler()). While this would be called before your window's PreviewKeyDown handler, it will be called for all TextBoxes in your application. This may or not be what you want.

Upvotes: 1

Related Questions