Jason Young
Jason Young

Reputation: 3743

WPF Accept input without focus

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.

Upvotes: 3

Views: 2876

Answers (3)

Alan Jackson
Alan Jackson

Reputation: 6511

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.

Upvotes: 0

Reed Copsey
Reed Copsey

Reputation: 564413

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.

Upvotes: 5

Sushant
Sushant

Reputation: 1

We just have to set the button attributes for enable those features.
Accept Button - Button.IsDefault = "True"
Cancel Button - Button.IsCancel = "True"

Upvotes: 0

Related Questions