views:

1476

answers:

3

This other SO question asks about an autocomplete textbox in WPF. Several people have built these, and one of the answers given there suggests this codeproject article.

But I've not found any WPF Autocomplete Textbox that compares with the WinForms autocomplete textbox. The codeproject sample works, sort of, ...

alt text

...but

  • it isn't structured as a re-usable control or DLL. It's code I need to embed in every app.
  • 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.
  • it doesn't have properties to set the popup size, etc.
  • 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.
  • navigating focus away from the listbox doesn't make the popup listbox disappear. This is confusing.

So, my question:

*Does anyone have a FREE WPF AutoComplete textbox that works, and provides a quality UI experience?*


ANSWER

Here's how I did it:

.1. run the MSI for the WPF Toolkit

.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:

alt text

If you don't want to use the designer, hand-craft the xaml. It looks like this:


<toolkit:AutoCompleteBox
   ToolTip="Enter the path of an assembly."
   x:Name="tbAssembly" Height="27" Width="102"
   Populating="tbAssembly_Populating" />

...where the toolkit namespace is mapped this way:

xmlns:toolkit="clr-namespace:System.Windows.Controls;assembly=System.Windows.Controls.Input.Toolkit"

.3. Provide the code for the Populating event. Here's what I used:


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<string>();

        Array.ForEach(new String[][] { files, dirs }, (x) =>
            Array.ForEach(x, (y) =>
                      {
                          if (y.StartsWith(dirname, StringComparison.CurrentCultureIgnoreCase))
                              candidates.Add(y);
                      }));

        tbAssembly.ItemsSource = candidates;
        tbAssembly.PopulateComplete();
    }
}

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:

alt text


Thanks to Matt for the pointer to the WPF toolkit.

+5  A: 

The newest drop of the WPF Toolkit includes an AutoCompleteBox. It's a free set of controls from Microsoft, some of which will be included in .NET 4.

Jeff Wilcox - Introducing the AutoCompleteBox

Matt Hamilton
Sounds intriguing, Matt. Is there a guide or example on using the AutoCompleteBox? I found the DLL that contains it. What xmlns do I use in XAML? How do I actually use it?
Cheeso
Note that the blog post link I just added talks about Silverlight, but AutoCompleteBox is for WPF too.
Matt Hamilton
+1  A: 

Here's how I did it:

.1. run the MSI for the WPF Toolkit

.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:

alt text

Or, hand-craft the xaml. It looks like this:


<toolkit:AutoCompleteBox
   ToolTip="Enter the path of an assembly."
   x:Name="tbAssembly" Height="27" Width="102"
   Populating="tbAssembly_Populating" />

...where the toolkit namespace is mapped this way:

xmlns:toolkit="clr-namespace:System.Windows.Controls;assembly=System.Windows.Controls.Input.Toolkit"

.3. Provide the code for the Populating event. Here's what I used:


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<string>();

        Array.ForEach(new String[][] { files, dirs }, (x) =>
            Array.ForEach(x, (y) =>
                      {
                          if (y.StartsWith(dirname, StringComparison.CurrentCultureIgnoreCase))
                              candidates.Add(y);
                      }));

        tbAssembly.ItemsSource = candidates;
        tbAssembly.PopulateComplete();
    }
}

Thanks to Matt for the pointer to the WPF toolkit.

Cheeso
A: 

I don't know why, but I'm not able to add the WPFToolkit controls to my toolbox in VS (and I deleted all of my WPF controls from the toolbox in the process, and I have not figured yet how to recover it, but that's not the question here.) In the meantime, could you please copy/paste here the XAML code of your project?

(I don't know why, but I can use the Calendar and DatePicker controls, but not the AutoCompleteBox one...)

Thanks in advance.

Elhu
I see you're new to Stackoverflow. If you have a new or followup question, don't post it as an answer to an existing question. instead, open new thread.
Cheeso