Building a custom AutoComplete TextBox in WPF requires combining a TextBox for user input and a floating Popup containing a ListBox to display the filtered suggestions. WPF does not include a native standalone autocomplete text control out of the box, so creating a reusable UserControl is the cleanest approach.
This implementation covers the XAML layout, C# code-behind with a dependency property, and data filtering logic. 1. The XAML Layout (AutoCompleteTextBox.xaml)
The layout embeds a Popup layout directly underneath the TextBox. The Popup uses PlacementTarget to track the position of the TextBox and automatically sizes itself to match.
Use code with caution. 2. The Code-Behind (AutoCompleteTextBox.xaml.cs)
The code-behind needs to expose a DependencyProperty so developers can bind a list of strings from their ViewModels. It also manages text filtering and keyboard navigation.
using System; using System.Collections; using System.Collections.Generic; using System.Linq; using System.Windows; using System.Windows.Controls; using System.Windows.Input; namespace WpfApp.Controls { public partial class AutoCompleteTextBox : UserControl { public AutoCompleteTextBox() { InitializeComponent(); } // Dependency Property to allow item binding from external sources public static readonly DependencyProperty SuggestionsSourceProperty = DependencyProperty.Register( nameof(SuggestionsSource), typeof(IEnumerable), typeof(AutoCompleteTextBox), new PropertyMetadata(null)); public IEnumerable SuggestionsSource { get => (IEnumerable)GetValue(SuggestionsSourceProperty); set => SetValue(SuggestionsSourceProperty, value); } // Triggered whenever the user types into the box private void TxtInput_TextChanged(object sender, TextChangedEventArgs e) { string query = txtInput.Text; // Hide popup if the input is empty or source data is missing if (string.IsNullOrWhiteSpace(query) || SuggestionsSource == null) { popupSuggestions.IsOpen = false; return; } // Filter data source (case-insensitive contains) var filtered = SuggestionsSource.Cast Use code with caution. 3. How to Consume Your Custom Control
Once built, add the local namespace to your target Window/Page XAML and pass your data source directly to the SuggestionsSource property.
Use code with caution. Architectural Upgrades for Production Use
If you need a highly responsive or complex control, scale this pattern using the following techniques:
Leave a Reply