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.
ARidder101 Jan 17 '17 at 19:36 2017-01-17 19:36
source share