AutoFill text field in WPF

Can I autofill a text field in WPF?

I found a pattern that uses a combo box and the triangle is removed by editing the style template.

Is there a better solution?

+47
autocomplete wpf wpf-controls textbox
Jun 04 '09 at 2:00
source share
6 answers

You can find it in the WPF Toolkit , which is also available through NuGet.

This article shows how to create a text field that can automatically indicate items at run time based on the input, in this case, of the disk folders. WPF autocomplete text folder

Also take a look at this nice multiple WPF Autocomplete TextBox , it was very convenient for me.

+32
Nov 19 '09 at 11:25
source share
— -

Nimgoble's is the version I used in 2015. Thought I'd say this question was the top of the list on google for “wpf autocomplete text box”,

  1. Installing the nuget package for a project in Visual Studio

  2. Add a link to the library in xaml:
    xmlns:behaviors="clr-namespace:WPFTextBoxAutoComplete;assembly=WPFTextBoxAutoComplete"

  3. Create a text box and bind AutoCompleteBehaviour to List<String> (TestItems):
    <TextBox Text="{Binding TestText, UpdateSourceTrigger=PropertyChanged}" behaviors:AutoCompleteBehavior.AutoCompleteItemsSource="{Binding TestItems}"/>

IMHO it is much easier to start and manage than the other options listed above.

+10
Jul 24 '15 at 6:40
source share

I published WPF Auto Complete Text Box in WPF on CodePlex.com. You can download and try it from https://wpfautocomplete.codeplex.com/ .

+8
Jul 29 '14 at 11:33
source share

or you can add an AutoCompleteBox to the toolbar by clicking on it and then select "Elements", go to the WPF components, enter the AutoCompleteBox filter, which is located in the System.Windows.Controls namespace and just drag and drop into your xaml file, It’s much easier than doing these other things since AutoCompleteBox is a built-in control.

+4
Nov 30 '12 at 19:22
source share

and here is the instrument plug that contains port 4.O,

https://github.com/jogibear9988/wpftoolkit

It worked very well for me.

+3
Feb 19 '14 at 16:49
source share

I know this is a very old question, but I want to add the answer that I came up with.

First you need a handler for your regular TextChanged event TextChanged for a TextBox :

 private bool InProg; internal void TBTextChanged(object sender, TextChangedEventArgs e) { var change = e.Changes.FirstOrDefault(); if ( !InProg ) { InProg = true; var culture = new CultureInfo(CultureInfo.CurrentCulture.Name); var source = ( (TextBox)sender ); if ( ( ( change.AddedLength - change.RemovedLength ) > 0 || source.Text.Length > 0 ) && !DelKeyPressed ) { if ( Files.Where(x => x.IndexOf(source.Text, StringComparison.CurrentCultureIgnoreCase) == 0 ).Count() > 0 ) { var _appendtxt = Files.FirstOrDefault(ap => ( culture.CompareInfo.IndexOf(ap, source.Text, CompareOptions.IgnoreCase) == 0 )); _appendtxt = _appendtxt.Remove(0, change.Offset + 1); source.Text += _appendtxt; source.SelectionStart = change.Offset + 1; source.SelectionLength = source.Text.Length; } } InProg = false; } } 

Then create a simple PreviewKeyDown handler:

  private static bool DelKeyPressed; internal static void DelPressed(object sender, KeyEventArgs e) { if ( e.Key == Key.Back ) { DelKeyPressed = true; } else { DelKeyPressed = false; } } 

In this example, “Files” is a list of directory names created at application startup.

Then just attach the handlers:

 public class YourClass { public YourClass() { YourTextbox.PreviewKeyDown += DelPressed; YourTextbox.TextChanged += TBTextChanged; } } 

In this case, everything that you decide to put in the List will be used for the autocomplete field. This may not be a great option if you expect to have a huge list for autocomplete, but in my application it ever sees 20-50 elements, so it cycles very quickly.

+3
Jan 17 '17 at 19:36
source share



All Articles