<ComboBox x:Name="cbCustomVar" Grid.Row="0" Grid.Column="0" VerticalAlignment="Center" Height="30" ItemsSource="{Binding SpecificationVM.Specifications}" IsEditable="True" KeyUp="CbCustomVar_KeyUp"></ComboBox>
防止控件自动清空解决办法
public ObservableCollection<string> dicCustomVars { get; set; } = new ObservableCollection<string>();
string str;
private async void CbCustomVar_KeyUp(object sender, System.Windows.Input.KeyEventArgs e)
{
await Task.Delay(500);
str = cbCustomVar.Text.ToString();
if (string.IsNullOrEmpty(str))
{
cbCustomVar.ItemsSource = SpecificationVM.Specifications;
return;
}
dicCustomVars.Clear();
// System.Threading.Thread.Sleep(500);
foreach (var customVar in SpecificationVM.Specifications)
{
if(customVar.Contains(str))
{
dicCustomVars.Add(customVar);
}
}
cbCustomVar.ItemsSource = "";
if (dicCustomVars.Count > 0)
{
cbCustomVar.ItemsSource = dicCustomVars;
// this.cbCustomVar.SelectedIndex = -1;
}
else
{
//cbCustomVar.ItemsSource = SpecificationVM.Specifications;
}
cbCustomVar.Text=str;
cbCustomVar.IsDropDownOpen = true;
}