Announcement: This repository will soon be renamed to a shorter name.
A lightweight ComboBox control that supports filtering (auto completion).
Declare XML namespace.
<Window
...
xmlns:dotNetKitControls="clr-namespace:DotNetKit.Windows.Controls;assembly=DotNetKit.Wpf.AutoCompleteComboBox"
... >You can then use AutoCompleteComboBox just like a normal ComboBox, since it inherits from it.
<dotNetKitControls:AutoCompleteComboBox
SelectedValuePath="Id"
TextSearch.TextPath="Name"
ItemsSource="{Binding Items}"
SelectedItem="{Binding SelectedItem}"
SelectedValue="{Binding SelectedValue}"
/>Note that:
- Set a property path to
TextSearch.TextPathproperty.- The path should point to a property that returns a string used to identify items. For example, if each item is a
Personobject with aNameproperty, andTextSearch.TextPathis set to"Name", typing"va"will filter out all items whoseNamedoesn't contain"va". - No support for
TextSearch.Text.
- The path should point to a property that returns a string used to identify items. For example, if each item is a
- Don't use
ComboBox.Itemsproperty directly. UseItemsSourceinstead. - Although the Demo project uses DataTemplate to display items, you can also use
DisplayMemberPath.
The default settings should work well for most cases, but you can customize the behavior if needed.
- Define a class derived from DotNetKit.Windows.Controls.AutoCompleteComboBoxSetting to override some of its properties.
- Set the instance to
AutoCompleteComboBox.Settingproperty.
<dotNetKitControls:AutoCompleteComboBox
Setting="..."
...
/>- Or set
AutoCompleteComboBoxSetting.Defaultto apply to all comboboxes.
Filtering allows you to add many items without losing usability, but it can affect performance. To improve this, we recommend using VirtualizingStackPanel as the items panel.
Use the ItemsPanel property:
<dotNetKitControls:AutoCompleteComboBox ...>
<dotNetKitControls:AutoCompleteComboBox.ItemsPanel>
<ItemsPanelTemplate>
<VirtualizingStackPanel />
</ItemsPanelTemplate>
</dotNetKitControls:AutoCompleteComboBox.ItemsPanel>
</dotNetKitControls:AutoCompleteComboBox>or declare a style in resources as the Demo app does.
See also WPF: Using a VirtualizingStackPanel to Improve ComboBox Performance for more detailed explanation.
- Multiple ComboBoxes can affect each other if they share the same ItemsSource instance.
- Workaround: Use distinct ItemsSource instance for each AutoCompleteComboBox. For example, wrap it with a ReadOnlyCollection.
- Changing
AutoCompleteComboBox.Filterin user code conflicts with the control's internal behavior. - Workaround: Avoid changing Filter in user code. Filter ItemsSource instead.
- There seems to be no reliable way to merge CollectionView filters. Please let me know if you have a solution.
ComboBox doesn't appear to support the Background property. No easy fix is known.
This library is essentially a thin wrapper around the standard ComboBox with additional behaviors.
- Sets
IsEditableto true to allow text input - Finds the TextBox part (in the ComboBox) to listen to the TextChanged event
- Opens or closes the dropdown when the text changes (after the debounce timer fires)
- TextBox selection is carefully saved and restored to not disturb the user
- Filters the ComboBox items based on the input
- Handles PreviewKeyDown events (
Ctrl+Space) to open the dropdown
