I am using a Silverlight usercontrol in my ASP.NET web app.
The user control has several autocomplete boxes and it seems that the enter key never fires the keydown event in any of them while it fires for other keys.
I’m assuming that autocomplete boxes must handle the enter key in a different way, perhaps for chosing an item from the list. – Thus it works with simple text boxes.
I was thinking about overriding the eventhandler in a new deriving control…
Have you guys found a solution for this?
Joe White is correct that some controls handle key events on their own, which has the effect of masking them to higher-level controls. If you take a look at the AutoCompleteBox in Reflector you will see that Enter, Escape, and F4 all cause something to happen and mark e.Handled = true.
Unfortunately PreviewKeyDown does not exist in the Silverlight world.
One way I have been able to prevent controls from responding to and capturing these key events is by subclassing the control and overriding the OnKeyDown method. Something like this would allow you to control whether or not the control reacts to the key events:
You could then use the the HandleKeyEvents property in XAML to disable the control from handling them:
This type of thing would prevent the base AutoCompleteBox from ever marking e.Handled = true and allow the event to bubble so your higher-level control could do something else with it. You could get more specific with which keys were handled if you wanted to prevent other KeyDown events (besides Enter) from breaking.