WPF: AutoComplete TextBox, ...again
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
I use the Intellibox in my in-house project. http://intellibox.codeplex.com/
I find it's use of the Provider pattern for searching very intuitive.
Rake's answer provides an example of how to use it, and as he points out, it has seen some development late last year (although this is well after I last used it).
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:
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.
Mindscape also provides a 3 free controls including a WPF Autocomplete Textbox
http://intellibox.codeplex.com/ does seem to updated as recently as Oct 1, 2013 and contains the single control. I would have added as comment on Troy's answer, but don't have enough rep. I nearly ignored it because of that comment.
Example usage from documentation:
<auto:Intellibox ResultsHeight="80"
ExplicitlyIncludeColumns="True"
Name="lightspeedBox"
DisplayedValueBinding="{Binding Product_Name}"
SelectedValueBinding="{Binding Product_Id}"
DataProvider="{Binding RelativeSource={RelativeSource FindAncestor,
AncestorType={x:Type Window}}, Path=LinqToEntitiesProvider}"
Height="26"
Margin="12,26,12,0"
VerticalAlignment="Top">
<auto:Intellibox.Columns>
<auto:IntelliboxColumn DisplayMemberBinding="{Binding Product_Name}"
Width="150"
Header="Product Name" />
<auto:IntelliboxColumn DisplayMemberBinding="{Binding Unit_Price}"
Width="75"
Header="Unit Price" />
<auto:IntelliboxColumn DisplayMemberBinding="{Binding Suppliers.Company_Name}"
Width="125"
Header="Supplier" />
</auto:Intellibox.Columns>
</auto:Intellibox>