I am looking to find a generic way to support keyboard wedge scanning for my WPF TextBox controls.
(I am really a novice when it comes to more advanced WPF features, so I would like to ask if I am going in the right direction before I put a lot of time into research.)
What I am wanting to do is to add an Attached Property (or something) to my TextBoxes that will cause it to read all input into the box and then call a custom “ScanCompleted” command with the scanned input.
If an Attached Property is not a good fit for this, then is there a way to get this command on a TextBox without descending my own custom “ScanableTextBox”?
(Note: The criteria for a scan (instead of typed data) is that it will start with the Pause key (#19) and end with a Return key (#13).)
I think this could probably be accomplished with attached properties (behaviors), but would be much simpler and more straightforward to simply subclass
TextBoxand override theOnTextChanged,OnKeyDown,OnKeyUpand similar methods to add custom functionality.Why don’t you want to create your own control in this way?
update: Attached Behaviour
If you really don’t want a derived control, here is an attached behaviour that accomplishes this (explanation below):
Consume this by declaring a
TextBoxlike so (wherelocalis the namespace of your attached property, andScanCompletedis anICommandon your view-model):Now when this property is set, we add the
TextBoxto a static collection along with its associatedICommand.Each time a key is pressed, we check whether it is the Pause key. If it is, and if the
TextBoxis empty, we set a flag totrueto start looking for the Enter key.Now each time a key is pressed, we check whether it is the Enter key. If it is, we execute the command, passing in the
TextBox.Textvalue, and reset the flag tofalsefor thatTextBox.We’ve also added a handler for the
TextBox.Unloadedevent to clean up our event subscriptions and remove theTextBoxfrom the static list.