title: WPF: AutoComplete TextBox, ...again
body: <p><a href="https://stackoverflow.com/questions/950770/autocomplete-textbox-in-wpf">This other SO question</a> asks about an autocomplete textbox in WPF.  Several people have built these, and one of the answers given there suggests <a href="http://www.codeproject.com/KB/WPF/autocomplete-textbox.aspx" rel="nofollow noreferrer">this codeproject article</a>.</p>
  
  <p>But I've not found any WPF Autocomplete Textbox that compares with the WinForms autocomplete textbox.  The codeproject sample works, sort of, ...</p>
  
  <p><a href="https://i.stack.imgur.com/VBMV8.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/VBMV8.png" alt="alt text"></a>
  </p>
  
  <p>...but</p>
  
  <ul>
  <li>it isn't structured as a re-usable control or DLL.  It's code I need to embed in every app. </li>
  <li>It works only with directories.  it doesn't have properties for setting whether the autocomplete source is filesystem directories only, or filesystem files, or ....etc.  I could write code to do this, of course, but...I'd rather use someone else's code already written.  </li>
  <li>it doesn't have properties to set the popup size, etc.</li>
  <li>there's a popup listbox that presents the posible completions. When navigating through that list, the textbox doesn't change.  Typing a character while focused in the listbox doesn't cause the textbox to get updated. </li>
  <li>navigating focus away from the listbox doesn't make the popup listbox disappear. This is confusing. </li>
  </ul>
  
  <p>So, my question:</p>
  
  <p>*Does anyone have a FREE WPF AutoComplete textbox <strong><em>that works</em></strong>, and provides a quality UI experience?*</p>
  
  <hr>
  
  <p><strong>ANSWER</strong></p>
  
  <p>Here's how I did it: </p>
  
  <p>.0. get the <a href="http://wpf.codeplex.com/releases/view/40535" rel="nofollow noreferrer">WPF Toolkit</a></p>
  
  <p>.1. run the MSI for the WPF Toolkit</p>
  
  <p>.2. Within Visual Studio, Drag/drop from the toolbox - specifically the Data Visualization group - into the UI Designer.  It looks like this in the VS toolbox: </p>
  
  <p><a href="https://i.stack.imgur.com/cdK7o.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/cdK7o.png" alt="alt text"></a>
  </p>
  
  <p>If you don't want to use the designer, hand-craft the xaml.  It looks like this: </p>
  
  <hr>
  
  <pre><code>&lt;toolkit:AutoCompleteBox
     ToolTip="Enter the path of an assembly."
     x:Name="tbAssembly" Height="27" Width="102"
     Populating="tbAssembly_Populating" /&gt;
  </code></pre>
  
  <p>...where the toolkit namespace is mapped this way: </p>
  
  <pre><code>xmlns:toolkit="clr-namespace:System.Windows.Controls;assembly=System.Windows.Controls.Input.Toolkit"
  </code></pre>
  
  <hr>
  
  <p>.3. Provide the code for the <code>Populating</code> event.  Here's what I used: </p>
  
  <hr>
  
  <pre><code>private void tbAssembly_Populating(object sender, System.Windows.Controls.PopulatingEventArgs e)
  {
      string text = tbAssembly.Text;
      string dirname = Path.GetDirectoryName(text);
  
      if (Directory.Exists(Path.GetDirectoryName(dirname)))
      {
          string[] files = Directory.GetFiles(dirname, "*.*", SearchOption.TopDirectoryOnly);
          string[] dirs = Directory.GetDirectories(dirname, "*.*", SearchOption.TopDirectoryOnly);
          var candidates = new List&lt;string&gt;();
  
          Array.ForEach(new String[][] { files, dirs }, (x) =&gt;
              Array.ForEach(x, (y) =&gt;
                        {
                            if (y.StartsWith(dirname, StringComparison.CurrentCultureIgnoreCase))
                                candidates.Add(y);
                        }));
  
          tbAssembly.ItemsSource = candidates;
          tbAssembly.PopulateComplete();
      }
  }
  </code></pre>
  
  <hr>
  
  <p>It works, just the way you'd expect.  It feels professional. There are none of the anomalies that the codeproject control exhibits. This is what it looks like: </p>
  
  <p><a href="https://i.stack.imgur.com/pclvE.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/pclvE.png" alt="alt text"></a>
  </p>
  
  <hr>
  
  <p><a href="https://stackoverflow.com/questions/2338690/wpf-autocomplete-textbox-again/2338707#2338707">Thanks to Matt for the pointer</a> to the WPF toolkit.</p>