views:

321

answers:

2

I've created a screen in WPF that accepts input from a barcode scanner. I listen to PreviewTextInput event, and the KeyUp event, which let's me determine what the scanner "typed", and when it finished. It works great. You just have to have the form displayed and it will accept barcode scans.

The problem is, I have other controls on the page, and they're causing issues. For example, when you push a button on the form, that button then has focus. Hitting the enter key from that point on results in the button event handler being fired. It doesn't automatically give up focus.

Does anyone know an elegant solution to this type of problem? I'd rather not add code to every event handler to focus another element, but I will do that as a last resort.

A: 

The PreviewTextInput event could change the focus (defocus if a button has it) so that when the enter key at the end of a barcode hits, the button wouldn't get pressed.

Alan Jackson
A: 

Button (actually UIElement) has a property for this specific purpose: UIElement.Focusable.

If you set this to false in your xaml or code, you can still click on the button, but it will not take focus away from other UIElements.

Reed Copsey
That may just work, I'll brb.
Jason Young