4

I have a ComboBox in my WPF App, that I want to enter Text in, which is used to update the Content (Items) of the ComboBox.

Whenever I enter a character, I want the updated Items to show. So I set IsDropDownOpen = true;, but this causes the Text which was entered to be selected.

The result is: I type 1 char, the ComboBox DropDown is opened (and shows new Items), the char gets selected. I type a second char and it replaces the first one, because it was selected (duh)!

How can I deselect all Text in the ComboBox? I obviously want to type more than 1 char whithout having to click on the ComboBox again to deselect all text...

Code:

//MainWindow.xaml
<ComboBox x:Name="comboBoxItemID" HorizontalAlignment="Left" Height="38"
 VerticalAlignment="Top" Width="300" Canvas.Left="197" FontSize="21.333"
 IsEnabled="True" KeyUp="comboBoxItemID_KeyUp" IsEditable="True"/>

//comboBoxItemID_KeyUp()
List<InventorySearchResult> Items = Invent.SearchArticle(this.comboBoxItemID.Text); //Get new Data
comboBoxItemID.ItemsSource = Items;         //Update comboBox Data
comboBoxItemID.DisplayMemberPath = "Name";
comboBoxItemID.IsDropDownOpen = true;
H.B.
  • 166,899
  • 29
  • 327
  • 400
NoMad
  • 702
  • 2
  • 11
  • 27
  • Do you have to constantly change the itemssource, or can you have all the data in it to begin with? If the latter is the case, you can just do that and the search is built into the combobox – Gordon Allocman Apr 01 '16 at 12:55
  • It changes, based on what is typed in. `Invent.SearchArticle()` actually performs a SQL Query, the returned `List` is a list of Rows returned by that query. Or should I declare a List, that that as ItemsSource once, and only change that list's contents? – NoMad Apr 01 '16 at 13:02
  • I don't know if changing the contents would make a difference, it might. If you try it, switch to an observable collection otherwise your ui won't update and you will likely get exceptions thrown – Gordon Allocman Apr 01 '16 at 13:05
  • It probably isn't the best idea to query a database everytime a single key is pressed though – Gordon Allocman Apr 01 '16 at 13:06
  • Re-innovating [autocomplete](http://stackoverflow.com/q/522521/1997232)? – Sinatr Apr 01 '16 at 13:13
  • Autocomplete only works if the Application has all the Data already loaded. What if a new Item is added while typing? Is it better to fetch 10M Rows again? I'll add some timeout so SQL only gets queried every 100ms or so at most. But it's only 1 Query with 25 results from 2 Clients on a LAN. Still trying to figure out how to not select the Text when DropDown is opened. – NoMad Apr 01 '16 at 13:31
  • Does it need to open on input only? Otherwise i'd bind `IsDropDownOpen` to `IsKeyboardFocusWithin`, that way it opens on click and selects the text once. After that you just need to properly handle the items collection (filter and merge rather than replace). – H.B. Apr 01 '16 at 17:44
  • This is exactly what my problem is. I can't prepopulate my autocomplete because it would be 200,000 strings. I'm calling a WebAPI that does a type ahead search returning 10 strings. It's all working great except when I expand the dropdown it selects the text messing up the users next entry as they type. Did you ever find a solution? – Christopher Painter Oct 26 '18 at 15:16
  • @ChristopherPainter If your problem is just that the text is auto-selected, see my answer below. For some (weird) reason you have to manually de-select the text in the TextBox. But I don't know the underlying cause, why all text gets selected in the first place. – NoMad Oct 29 '18 at 09:29
  • Thanks. No matter what solution I tried in winforms I got unhandled exceptions thrown. I switched to WPF and the exceptions went away. I did have to delect the text and move the caret to the end. It's all working now. – Christopher Painter Oct 29 '18 at 11:23

1 Answers1

4

To deselect all Text from a ComboBox, first get the TextBox Element:

TextBox tb = (TextBox)comboBoxItemID.Template.FindName("PART_EditableTextBox", comboBoxItemID);

(source: How to get ComboBox.SelectedText in WPF )

Then use the TextBox method Select(int start, int length) to set the selection, e.g.

tb.Select(tb.Text.Length, 0);

(source: Deselect text in a textbox )

Finally, disable the IsTextSearchEnabled property. I haven't done much testing to find out why exactly this has to be disabled, but I guess in my case it conflicts with the way I update the Items and then deselect the Text.

Community
  • 1
  • 1
NoMad
  • 702
  • 2
  • 11
  • 27