У меня есть поле со списком национальностей, подобное приведенному ниже, и я хочу сделать так, чтобы пользователь мог набирать буквы для узких в вариантах. Я мог бы решить эту проблему так, как я начал ниже, добавив logic в методе NationalityComboBox_KeyDown.
Это лучший способ встроить AutoSuggest в ComboBox или есть встроенный способ сделать то же самое?
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"....
}
}