Autocomplete textbox in WPF (well, almost)

29 באוקטובר 2007

2 תגובות

[This blog was migrated. You will not be able to comment here.
The new URL of this post is http://khason.net/blog/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.

הוסף תגובה
facebook linkedin twitter email

כתיבת תגובה

האימייל לא יוצג באתר. (*) שדות חובה מסומנים

2 תגובות

  1. Zeeshan13 בפברואר 2008 ב 10:57

    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 ?

    להגיב