title: ComboBox Up/Down arrow keys issue after Items is repopulated
body: <p>I am creating a custom ComboBox control with Google Search results implementation but I am running into some problem using the arrow keys.</p>
  
  <p><strong>Details of the problem</strong><br/>
  Please watch this video as I demonstrate the problem with UP and DOWN arrow keys and take a look on the output panel as well. </p>
  
  <p><a href="http://screencast.com/t/DFkmlDKR" rel="nofollow noreferrer">http://screencast.com/t/DFkmlDKR</a></p>
  
  <p>In the video, I tried searching for "a" (just a test) and returns the Google search results that starts with "a" then I am pressing the DOWN and UP arrow keys. In the output panel, it shows the highlighted item as I press these keys.<br/>
  <img src="https://i.stack.imgur.com/dKF5E.png" alt="enter image description here"></p>
  
  <p>Then I typed the next letter "l". Now If you noticed on the output panel, the selection is now "null" but I'm still pressing the UP &amp; DOWN arrow keys.<br/>
  <img src="https://i.stack.imgur.com/DTCJU.png" alt="enter image description here"></p>
  
  <p>And when you hover on the item again using the mouse pointer, it will start working again.<br/>
  <img src="https://i.stack.imgur.com/v9YV7.png" alt="enter image description here"></p>
  
  <p>I am stuck with this problem for days and haven't figured out the solution.</p>
  
  <p>I have uploaded the test version of this control so you can play with it as well. Here it is <a href="http://sdrv.ms/1eWV3Bc" rel="nofollow noreferrer">GoogleSuggestionComboBox</a> <br/>
  <img src="https://i.stack.imgur.com/6rPbF.png" alt="enter image description here"></p>
  
  <p>My goal here is to make the UP &amp; DOWN arrow keys work all the time.</p>
  
  <p>In this section of the code
  <img src="https://i.stack.imgur.com/ilnSn.png" alt="enter image description here">
  I tried adding</p>
  
  <pre><code>SelectedIndex = 0;
  </code></pre>
  
  <p>after the ForEach statement so everytime the collection is repopulated with the new results, it will select the first result. Unfortunately, it did not work.</p>
  
  <p>You can download the test code so you can play and test the problem. <a href="http://sdrv.ms/1eWV3Bc" rel="nofollow noreferrer">http://sdrv.ms/1eWV3Bc</a> and here's the code for the ComboBox as well.</p>
  
  <pre><code>using GoogleSuggestionComboBox.Model;
  using System.Collections.Generic;
  using System.ComponentModel;
  using System.Diagnostics;
  using System.Windows;
  using System.Windows.Controls;
  using System.Windows.Input;
  
  namespace GoogleSuggestionComboBox
  {
      public class ComboBoxExC : ComboBox
      {
          GoogleSuggest google;
          TextBox textbox;
          string _text = string.Empty;
          string _last_text = string.Empty;
  
          public ComboBoxExC()
          {
              if (DesignerProperties.GetIsInDesignMode(this))
              {
  
              }
              else
              {
                  this.Loaded += ComboBoxExC_Loaded;
  
                  google = new GoogleSuggest();
                  google.OnGoogleSuggestAvailable += google_OnGoogleSuggestAvailable;
  
                  // since we have OnSelectionChanged "disabled"
                  // we need a way to know if the item in ComboBox is selected using mouse
                  EventManager.RegisterClassHandler(typeof(ComboBoxItem), ComboBoxItem.MouseDownEvent, new MouseButtonEventHandler(OnItemMouseDown));
              }
          }
  
          void ComboBoxExC_Loaded(object sender, RoutedEventArgs e)
          {
              this.textbox = (TextBox)Template.FindName("PART_EditableTextBox", this);
          }
  
          private void OnItemMouseDown(object sender, MouseButtonEventArgs e)
          {
              var comboBoxItem = sender as ComboBoxItem;
              if (comboBoxItem != null &amp;&amp; comboBoxItem.IsHighlighted)
              {
                  Model_SuggestedQueries m = (Model_SuggestedQueries)comboBoxItem.Content;
                  Go(m.Query);
              }
          }
  
          protected override void OnSelectionChanged(SelectionChangedEventArgs e)
          {
              // don't do anything so the .Text value won't change.
  
              //base.OnSelectionChanged(e);
          }
  
          protected override void OnPreviewKeyDown(KeyEventArgs e)
          {
              this.IsDropDownOpen = true;
  
              base.OnPreviewKeyDown(e);
  
              d("key: " + e.Key.ToString());
  
              if (this.SelectedItem != null)
              {
                  Model_SuggestedQueries m = (Model_SuggestedQueries)this.SelectedItem;
                  d("selected: " + m.Query);
              }
              else
              {
                  d("null");
              }
          }
  
          protected override void OnPreviewKeyUp(KeyEventArgs e)
          {
              base.OnPreviewKeyUp(e);
  
              if (e.Key == Key.Enter)
              {
                  if (this.SelectedItem == null)
                  {
                      Go(this.Text);
  
                  }
                  else
                  {
                      Model_SuggestedQueries m = (Model_SuggestedQueries)this.SelectedItem;
                      Go(m.Query);
                  }
              }
              else
              {
                  if (this.Text != this._last_text)
                  {
                      google.LookForSuggestion(this.Text);
  
                      this._last_text = this.Text;
                  }
              }
          }
  
          void google_OnGoogleSuggestAvailable(object sender, List&lt;Model.Model_SuggestedQueries&gt; suggestions)
          {
              this.Items.Clear();
  
              suggestions.ForEach((a) =&gt;
              {
                  this.Items.Add(a);
              });
          }
  
          void d(object a)
          {
              Debug.WriteLine("&gt;&gt;&gt; " + a);
          }
  
          void Go(string query)
          {
              Process.Start("https://www.google.com.ph/search?q=" + query);
  
              // clear suggestions
              this.Items.Clear();
          }
      }
  }
  </code></pre>
  
  <p>MainWindow.xaml</p>
  
  <pre><code>&lt;Window x:Class="GoogleSuggestionComboBox.MainWindow"
          xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
          xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
          xmlns:l="clr-namespace:GoogleSuggestionComboBox"
          Title="MainWindow" Height="133" Width="261" WindowStartupLocation="CenterScreen"
          &gt;
      &lt;Grid&gt;
          &lt;StackPanel Margin="10"&gt;
              &lt;TextBlock Text="Search" /&gt;
              &lt;l:ComboBoxExC 
                  IsEditable="True" 
                  IsTextSearchEnabled="False"
                  TextSearch.TextPath="Query"
                  &gt;
                  &lt;l:ComboBoxExC.ItemTemplate&gt;
                      &lt;DataTemplate&gt;
                          &lt;TextBlock Text="{Binding Query}" /&gt;
                      &lt;/DataTemplate&gt;
                  &lt;/l:ComboBoxExC.ItemTemplate&gt;
              &lt;/l:ComboBoxExC&gt;
          &lt;/StackPanel&gt;
      &lt;/Grid&gt;
  &lt;/Window&gt;
  </code></pre>
  
  <p>Thank you,<br/>
  Jayson</p>