A good product requires good user interaction. Products that are using search boxes will be expected to have auto-completion/suggestion feature. In WPF platform, the default text box control does not have any built-in auto-completion/suggestion feature. There are many 3rd party libraries out there which provide auto-complete/suggestion text box control as a replacement to the default WPF text box control. Most of these libraries are available in the paid version and those which are available for free, are difficult to get integrated with your existing code. So, I have created a very simple AutoComplete/Suggestion control for WPF platform without using any external library.
You can easily integrate my control in your project.
Today, I shall be demonstrating the creation of a text box with auto-complete/suggestion feature along with its integration within your code in WPF platform.
Prerequisites
Following are the prerequisites before you proceed any further in this tutorial.
  1. Knowledge of Windows Presentation Form (WPF).
  2. Knowledge of C# Programming.
  3. Knowledge of C# LINQ.
You can download the complete source code of this tutorial from the below link. The sample code is being developed in Microsoft Visual Studio 2017 Professional.
Let's begin now.
Step 1
Create a new WPF application project and name it "WPFAutoCompleteTextBox".
Step 2
Create the "Views\UserControls\AutoCompleteTextBoxUserControl.xaml" file and replace the following code in it.
  1. <UserControl x:Class="WPFAutoCompleteTextBox.Views.UserControls.AutoCompleteTextBoxUserControl"
  2. xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
  3. xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
  4. xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
  5. xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
  6. xmlns:local="clr-namespace:WPFAutoCompleteTextBox.Views.UserControls"
  7. mc:Ignorable="d"
  8. d:DesignHeight="480" d:DesignWidth="640">
  9. <Grid>
  10. <StackPanel Orientation="Vertical"
  11. VerticalAlignment="Center"
  12. HorizontalAlignment="Center">
  13. <!-- Text Box -->
  14. <Border x:Name="autoTextBoxBorder"
  15. Width="220"
  16. Height="50">
  17. <Border.Background>
  18. <ImageBrush ImageSource="/WPFAutoCompleteTextBox;component/Content/img/text-box_bg.png"/>
  19. </Border.Background>
  20. <TextBox x:Name="autoTextBox"
  21. Width="220"
  22. Height="50"
  23. FontSize="18"
  24. HorizontalAlignment="Center"
  25. VerticalAlignment="Center"
  26. BorderThickness="0"
  27. VerticalContentAlignment="Center"
  28. HorizontalContentAlignment="Center"
  29. Padding="0,0,0,0"
  30. Background="Transparent"
  31. Foreground="Black"
  32. TextChanged="AutoTextBox_TextChanged"/>
  33. </Border>
  34. <!-- Auto Suggestion box -->
  35. <Popup x:Name="autoListPopup"
  36. Visibility="Collapsed"
  37. Height="100"
  38. StaysOpen="False"
  39. Placement="Bottom">
  40. <ListBox x:Name="autoList"
  41. Visibility="Collapsed"
  42. Width="500"
  43. Height="100"
  44. SelectionChanged="AutoList_SelectionChanged" />
  45. </Popup>
  46. </StackPanel>
  47. </Grid>
  48. </UserControl>
In the above code, I have created my auto-complete/suggestion user control hierarchy from the built-in WPF control with default settings.
Step 3
Now, open the "Views\UserControls\AutoCompleteTextBoxUserControl.xaml.cs" file and replace with the following code.
  1. //-----------------------------------------------------------------------
  2. // <copyright file="AutoCompleteTextBoxUserControl.xaml.cs" company="None">
  3. // Copyright (c) Allow to distribute this code and utilize this code for personal or commercial purpose.
  4. // </copyright>
  5. // <author>Asma Khalid</author>
  6. //-----------------------------------------------------------------------
  7. namespace WPFAutoCompleteTextBox.Views.UserControls
  8. {
  9. using System;
  10. using System.Collections.Generic;
  11. using System.Linq;
  12. using System.Text;
  13. using System.Threading.Tasks;
  14. using System.Windows;
  15. using System.Windows.Controls;
  16. using System.Windows.Data;
  17. using System.Windows.Documents;
  18. using System.Windows.Input;
  19. using System.Windows.Media;
  20. using System.Windows.Media.Imaging;
  21. using System.Windows.Navigation;
  22. using System.Windows.Shapes;
  23. /// <summary>
  24. /// Interaction logic for Autocomplete Text Box UserControl
  25. /// </summary>
  26. public partial class AutoCompleteTextBoxUserControl : UserControl
  27. {
  28. #region Private properties.
  29. /// <summary>
  30. /// Auto suggestion list property.
  31. /// </summary>
  32. private List<string> autoSuggestionList = new List<string>();
  33. #endregion
  34. #region Default Constructor
  35. /// <summary>
  36. /// Initializes a new instance of the <see cref="AutoCompleteTextBoxUserControl" /> class.
  37. /// </summary>
  38. public AutoCompleteTextBoxUserControl()
  39. {
  40. try
  41. {
  42. // Initialization.
  43. this.InitializeComponent();
  44. }
  45. catch (Exception ex)
  46. {
  47. // Info.
  48. MessageBox.Show(ex.Message, "Error", MessageBoxButton.OK, MessageBoxImage.Error);
  49. Console.Write(ex);
  50. }
  51. }
  52. #endregion
  53. #region Protected / Public properties.
  54. /// <summary>
  55. /// Gets or sets Auto suggestion list property.
  56. /// </summary>
  57. public List<string> AutoSuggestionList
  58. {
  59. get { return this.autoSuggestionList; }
  60. set { this.autoSuggestionList = value; }
  61. }
  62. #endregion
  63. #region Open Auto Suggestion box method
  64. /// <summary>
  65. /// Open Auto Suggestion box method
  66. /// </summary>
  67. private void OpenAutoSuggestionBox()
  68. {
  69. try
  70. {
  71. // Enable.
  72. this.autoListPopup.Visibility = Visibility.Visible;
  73. this.autoListPopup.IsOpen = true;
  74. this.autoList.Visibility = Visibility.Visible;
  75. }
  76. catch (Exception ex)
  77. {
  78. // Info.
  79. MessageBox.Show(ex.Message, "Error", MessageBoxButton.OK, MessageBoxImage.Error);
  80. Console.Write(ex);
  81. }
  82. }
  83. #endregion
  84. #region Close Auto Suggestion box method
  85. /// <summary>
  86. /// Close Auto Suggestion box method
  87. /// </summary>
  88. private void CloseAutoSuggestionBox()
  89. {
  90. try
  91. {
  92. // Enable.
  93. this.autoListPopup.Visibility = Visibility.Collapsed;
  94. this.autoListPopup.IsOpen = false;
  95. this.autoList.Visibility = Visibility.Collapsed;
  96. }
  97. catch (Exception ex)
  98. {
  99. // Info.
  100. MessageBox.Show(ex.Message, "Error", MessageBoxButton.OK, MessageBoxImage.Error);
  101. Console.Write(ex);
  102. }
  103. }
  104. #endregion
  105. #region Auto Text Box text changed the method
  106. /// <summary>
  107. /// Auto Text Box text changed method.
  108. /// </summary>
  109. /// <param name="sender">Sender parameter</param>
  110. /// <param name="e">Event parameter</param>
  111. private void AutoTextBox_TextChanged(object sender, TextChangedEventArgs e)
  112. {
  113. try
  114. {
  115. // Verification.
  116. if (string.IsNullOrEmpty(this.autoTextBox.Text))
  117. {
  118. // Disable.
  119. this.CloseAutoSuggestionBox();
  120. // Info.
  121. return;
  122. }
  123. // Enable.
  124. this.OpenAutoSuggestionBox();
  125. // Settings.
  126. this.autoList.ItemsSource = this.AutoSuggestionList.Where(p => p.ToLower().Contains(this.autoTextBox.Text.ToLower())).ToList();
  127. }
  128. catch (Exception ex)
  129. {
  130. // Info.
  131. MessageBox.Show(ex.Message, "Error", MessageBoxButton.OK, MessageBoxImage.Error);
  132. Console.Write(ex);
  133. }
  134. }
  135. #endregion
  136. #region Auto list selection changed method
  137. /// <summary>
  138. /// Auto list selection changed method.
  139. /// </summary>
  140. /// <param name="sender">Sender parameter</param>
  141. /// <param name="e">Event parameter</param>
  142. private void AutoList_SelectionChanged(object sender, SelectionChangedEventArgs e)
  143. {
  144. try
  145. {
  146. // Verification.
  147. if (this.autoList.SelectedIndex <= -1)
  148. {
  149. // Disable.
  150. this.CloseAutoSuggestionBox();
  151. // Info.
  152. return;
  153. }
  154. // Disable.
  155. this.CloseAutoSuggestionBox();
  156. // Settings.
  157. this.autoTextBox.Text = this.autoList.SelectedItem.ToString();
  158. this.autoList.SelectedIndex = -1;
  159. }
  160. catch (Exception ex)
  161. {
  162. // Info.
  163. MessageBox.Show(ex.Message, "Error", MessageBoxButton.OK, MessageBoxImage.Error);
  164. Console.Write(ex);
  165. }
  166. }
  167. #endregion
  168. }
  169. }
In the above code, I have created a public property "AutoSuggestionList" to which you will provide your auto-suggestion list.
I have, then, created an "OpenAutoSuggestionBox(..)" method which will open the auto suggestion box as the end-user types in the text box.
Then, I have created a "CloseSuggestionBox(...)" method which will close the auto-suggestion box when the end-user selects the target suggested item from the auto-suggestion list.
Then, I have created "AutoTextBox_TextChanged(...)" method which will update the auto-suggestion list according to the text typed in the text box by the end-user.
Finally, I have created "AutoList_SelectionChanged(...)" method which will update the text box according to the selected suggested item from the auto-suggestion list by the end-user.
Step 4
In order to use my autocomplete/suggestion text box control, you need to include my user control namespace into your target XAML file and the following line of code.
You also need to set your auto-suggestion list in the target XAML.CS file.
Step 5
Now, execute the project and you will be able to see the following in action.

Conclusion

In this article, we learned to create an auto-complete/auto-suggestion text box control using the built-in UI controls in the WPF platform. Along with this, we learned how to integrate this auto-complete/suggestion text box user control into our own target WPF page.
Disclaimer
The background image used in this article has been taken from freepike.