以下のような Nationality ComboBox があり、ユーザーが文字を入力して選択肢を絞り込めるようにしたいと考えています。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>
コード ビハインド:
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"....
}
}