0

コンボボックスで利用可能なオプションを動的に更新しながら、編集可能なコンボボックスをユーザー入力の受け入れと保持の両方にするにはどうすればよいですか?

私が達成しようとしているのは、ユーザーがクエリの入力を開始できるようにし、これまでに入力した内容に基づいてクエリの候補を表示できるようにすることです。提案の取得とコモボボックスの内容の更新は順調に進んでいますが、更新のたびに入力が消去され、更新されたリストの最初のエントリに置き換えられます。

これが私がこれまでに試したことです(うまくいかなかった他の同様の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;
}
4

2 に答える 2

0

の更新は、ObservableCollection私が見ていた動作とは何の関係もないことがわかりました。後で、入力がドロップダウン コレクション内の一致するエントリを検索しているかのように動作し、ユーザーが毎回提供した入力を置き換えていることに気付きました。

それがまさに起こっていたことでした。IsTexSearchEnabledウィンドウXAMLで要素の属性をfalseに設定するとComboBox、問題が解決しました。

<ComboBox x:Name="cmboSearchField" Margin="218,10,43,0" 
 IsTextSearchEnabled="false" VerticalAlignment="Top" 
 IsEditable="True" ItemsSource="{Binding SearchTopics}" 
 KeyUp="GetSearchTopics"/>
于 2013-05-01T17:13:26.917 に答える