Monday, October 29, 2007 1:07 PM
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.
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.
תגים:WPF, tutorial, Tips and Tricks