I have a Nationality ComboBox like the one below and want to make it so that the user can type letters to narrow in on the choices. I could solve this the way I started below by adding logic in the NationalityComboBox_KeyDown method.
Is this the best way to build AutoSuggest into a ComboBox or is there a built-in way to do the same thing?
XAML:
<ComboBox x:Class="TestComboSuggest343.NationalityComboBox"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Height="300" Width="300">
<ComboBox.ItemTemplate>
<DataTemplate>
<TextBlock Text="{Binding Value}"/>
</DataTemplate>
</ComboBox.ItemTemplate>
</ComboBox>
Code-Behind:
public partial class NationalityComboBox : ComboBox
{
public NationalityComboBox()
{
InitializeComponent();
Items.Add(new KeyValuePair<string, string>(null, "Please choose..."));
Items.Add(new KeyValuePair<string, string>(null, "American"));
Items.Add(new KeyValuePair<string, string>(null, "Australian"));
Items.Add(new KeyValuePair<string, string>(null, "Belgian"));
Items.Add(new KeyValuePair<string, string>(null, "French"));
Items.Add(new KeyValuePair<string, string>(null, "German"));
Items.Add(new KeyValuePair<string, string>(null, "Georgian"));
SelectedIndex = 0;
KeyDown += new KeyEventHandler(NationalityComboBox_KeyDown);
}
void NationalityComboBox_KeyDown(object sender, KeyEventArgs e)
{
SelectedIndex = 4; // can create logic here to handle key presses, e.g. "G", "E", "O"....
}
}