Provides a lightweight combobox with filtering (auto-complete).
Declare XML namespace.
<Window
...
xmlns:dotNetKitControls="clr-namespace:DotNetKit.Windows.Controls;assembly=DotNetKit.Wpf.AutoCompleteComboBox"
... >Then you can use AutoCompleteComboBox. It's like a normal ComboBox because of inheritance.
<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 leads to a property whose getter produces a string value to identify items. For example, assume each item is an instance of
Person, which hasNameproperty, and the property path is "Name". If the user input "va", the combobox filters the items to remove ones (persons) whoseNamedon't contain "va". - No support for
TextSeach.Text.
- The path leads to a property whose getter produces a string value to identify items. For example, assume each item is an instance of
- Don't use
ComboBox.Itemsproperty directly. UseItemsSourceinstead. - Although the Demo project uses DataTemplate to display items, you can also use
DisplayMemberPath.
This library works fine in the default setting, however, it also provides how to configure.
- Define a class derived from DotNetKit.Windows.Controls.AutoCompleteComboBoxSetting to override some of properties.
- Set the instance to
AutoCompleteComboBox.Settingproperty.
<dotNetKitControls:AutoCompleteComboBox
Setting="..."
...
/>- Or set to
AutoCompleteComboBoxSetting.Defaultto apply to all comboboxes.
Filtering allows you to add a lot of items to a combobox without loss of usability, however, that makes the performance poor. To get rid of the issue, we recommend you to use VirtualizingStackPanel as the panel.
Use 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.
This library is basically a thin wrapper of the standard ComboBox with some behaviors.
- Finds the TextBox part (in the ComboBox) to listen to the TextChanged event
- Opens or close the dropdown whenever the text changed (and then the debounce timer fired)
- TextBox selection is carefully saved and restored to not disturb the user
- Filters the ComboBox items based on the input
- Defines
ItemsSourceDependencyProperty that shadows theItemsControl.ItemsProperty(see also #26) - Handles PreviewKeyDown events (
Ctrl+Space) to open the dropdown
