コンボボックスで利用可能なオプションを動的に更新しながら、編集可能なコンボボックスをユーザー入力の受け入れと保持の両方にするにはどうすればよいですか?
私が達成しようとしているのは、ユーザーがクエリの入力を開始できるようにし、これまでに入力した内容に基づいてクエリの候補を表示できるようにすることです。提案の取得とコモボボックスの内容の更新は順調に進んでいますが、更新のたびに入力が消去され、更新されたリストの最初のエントリに置き換えられます。
これが私がこれまでに試したことです(うまくいかなかった他の同様のSOの提案とともに)
<ComboBox x:Name="cmboSearchField" Margin="197,10,0,0"
VerticalAlignment="Top" Width="310" IsTextSearchEnabled="True"
IsEditable="True" ItemsSource="{Binding SearchTopics}"
KeyUp="GetSearchTopics"/>
そして私のコードビハインド:
public ObservableCollection<string> SearchTopics {get;set;}
void GetSearchTopics(object sender, KeyEventArgs e)
{
bool showDropdown = this.cmboSearchField.IsDropDownOpen;
if ((e.Key >= Key.D0) && (e.Key <= Key.Z))
{
query = this.cmboSearchField.Text;
List<string> topics = GetQueryRecommendations(query);
_searchTopics.Clear();
_searchTopics.Add(query); //add the query back to the top
//stuffing the list into a new ObservableCollection always
//rendered empty when the dropdown was open
foreach (string topic in topics)
{
_searchTopics.Add(topic);
}
this.cmboSearchField.SelectedItem = query; //set the query as the current selected item
//this.cmboSearchField.Text = query; //this didn't work either
showDropdown = true;
}
this.cmboSearchField.IsDropDownOpen = showDropdown;
}