Autocomplete textbox in WPF (well, almost)

Autocomplete textbox is missing control in WPF 3.0 and WPF 3.5. Is it true? Actually, WPF is very extensible language, thus it is almost work nothing to build user control, derrived from TextBox to achive autocomplete functionality. But are we really have to do it? Let’s try to understand which control can provide us with similar functionality. I’m beting on ComboBox.

<

ComboBox>
 
<TextBlock>Hello</TextBlock>
 <TextBlock>World</TextBlock>
 <TextBlock>This</TextBlock>
 <TextBlock>is</TextBlock>
 <TextBlock>Autocomplete</TextBlock>
 <TextBlock>Textbox</TextBlock>
</ComboBox>

Now we have to make the combobox editable. So, let’s set IsEditable property to True. Ok, I can write, but can not select. ComboBox control provides us with option to choose items by typing its values. This works only when the ComboBox is opened. So we have to force the ComboBox to open its items and leave them opened while typing without pressing the arrow sign at the right. If I’ll bet on IsDropDownOpen property, I’ll be wrong, ‘cos I not need to see itesm, I want only select them. Another property looks interesting is IsTextSearchEnabled. Let’s see MSDN to understand what this property do… “Enables a user to quickly access items in a set by typing prefixes of strings”. Wow, that’s exactly what I need. But… what the default value? It’s True, so I’m not really need to set it. Let’s see, how can I force items to be opened while typing. Got it – StaysOpenOnEdit property.

Now, if I’ll put IsEditable=”True” StaysOpenOnEdit=”True” I’ll make my ComboBox to accept keystrokes to choose items without openning items panel. And that exactly what I need. So, now ComboBox with those two properties set to True converts into regular TextBox with autocomplition functionality. The only thing you should do (if you really want to), is to change control template to remove triangle button at right.

5 Responses to “Autocomplete textbox in WPF (well, almost)”

  1. Zeeshan Says:

    Good one but what if my list contains millions of records & i just need to display, let say, 10 of them at a time. Might need to write some code   for that ?

  2. Noticias externas Says:

    Autocomplete textbox is missing control in WPF 3.0 and WPF 3.5 . Is it true? Actually, WPF is very extensible

  3. עומר ון קלוטן Says:

    Very nice. :)

  4. Web 2.0 - Social Media - Internet News - Blogging » Autocomplete textbox in WPF (well, almost) Says:

    Pingback from  Web 2.0 – Social Media – Internet News – Blogging &raquo; Autocomplete textbox in WPF (well, almost)

  5. Tobias Twardon Says:

    Great one!

    Much simpler and sufficient than adding user control, libs, etc…!

    Cheers
    Tobias

Leave a Reply

Recommended

 


Sponsor


Partners

WPF Disciples
Dreamhost
Code Project
Switched to Better Place

Together