Click here to Skip to main content
14,743,899 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I have a search control based around a ComboBox. The control filters it's content according to the content of the text part of the ComboBox.

It all works as I want it to unless I hit the down arrow key in which case the first item in the filtered list is automatically selected - I don't want this to happen.

You'd think it would be a simple case of trapping the key event and marking it as handled but that's not working.

My ComboBox looks like this:

<ComboBox Name="cbUsers" VerticalAlignment="Top" HorizontalContentAlignment="Stretch" Grid.Row="1" Grid.Column="0" IsEditable="True" ItemsSource="{Binding RelativeSource={RelativeSource FindAncestor, AncestorType=my:Recipients, AncestorLevel=1}, Path=FilteredUsers}" IsTextSearchEnabled="False"  Focusable="True"   ScrollViewer.CanContentScroll="True" TextBoxBase.TextChanged="cbUsers_TextChanged" SelectionChanged="cbUsers_SelectionChanged" ToolTip="Begin typing name or enter initials to find user.">

What I have tried:

I've tried adding a PreviewKeyUp to the ComboBox and setting e.Handled to true if the key is the down arrow. This fires but has no effect.

I've also tried adding a handler directly to the TextBox portion:

TextBox textBox = (TextBox)cbUsers.Template.FindName("PART_EditableTextBox", cbUsers);
textBox.PreviewKeyUp += new System.Windows.Input.KeyEventHandler(textBox_PreviewKeyUp);

Again this fires but doesn't stop the automatic selection.
Updated 1-Sep-16 5:33am

1 solution

The reason why adding a delegate onto the preview up event is it's a multicast delegate - they all get fired. With routed events you can stop other events from firing by setting the event args to handled.

If you're not familiar with routing strategies look those up briefly, but in the case of preview key events the routing strategy is tunneling. That means if your ComboBox is attached to the key events it's events will fire first.

Sure enough if you preview the key up event on hitting a down arrow it will never get fired. Something is previewing that event, recognizing it's a down arrow, and setting handled to true. Makes sense in this case normally right?

Oddly I couldn't get it to stop the event with the template at the ComboBox level. Not sure if I missed something, but I added a preview key event for the Window and it intercepted it just fine. It would be best to put it the ComboBox inside another container and intercept at that level, but if you use something like this it can help:

if (e.Source is ComboBox && e.Key == Key.Down)
    e.Handled = true;

NOTE: When debugging the preview key events in a container I was tossing errors after continuing - odd. Still worked though...
PeejayAdams 2-Sep-16 5:02am
Many thanks for that, PureNsanity. I've been somewhat sidetracked from the issue in question so haven't had a chance to readdress but I very much suspect that you've put me on the right track - will come back and accept as answer when I get the chance to go back and look at it but in the meantime, thanks again and +5!

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900