我有国家名称的组合框我想设置搜索框以便轻松找到国家我该怎么做
这是我的ComboBox XAML编码
<ComboBox x:Name="countryComboBox" Loaded="countryComboBox_Loaded" Width="300"/>
这里是c#编码
private void countryComboBox_Loaded(object sender, RoutedEventArgs e)
{
countryComboBox.ItemsSource = GetCountries();
}
国家/地区列表正在加载,但没有文字区域,我想添加它以轻松找到该国家/地区。
答案 0 :(得分:0)
像这样的东西(可能有编译错误,我在记事本中写代码而不是VS)
<StackPanel>
<TextBox x:Name="searchBox" TextChanged="searchBox_TextChanged" />
<ComboBox x:Name="countryComboBox" Loaded="countryComboBox_Loaded" Width="300"/>
</StackPanel>
List<string> countries;
private void countryComboBox_Loaded(object sender, RoutedEventArgs e)
{
countries = GetCountries();
countryComboBox.ItemsSource = countries;
}
private void searchBox_TextChanged(object sender, TextChangedEventArgs e)
{
if (searchBox.Text == "") return;
int index = countries.FindIndex((str) => str.StartsWith(searchBox.Text));
if (index != -1)
countryComboBox.SelectedIndex = index;
}