Click here to Skip to main content
15,881,715 members
Articles / Desktop Programming / WPF

WPF AutoComplete TextBox

Rate me:
Please Sign up or sign in to vote.
3.95/5 (9 votes)
6 Apr 2016CPOL1 min read 17.5K   851   7   4
Light AutoComplete TextBox

Introduction

This is about the auto completed search text box. This loads the IEnumerable values and the user shall do containing search. When the user types the letter/word, it displays all the contained values in the drop down. The search is case insensitive.

How It Differs from Editable WPF ComboBox?

  1. This is light search textbox
  2. This displays values in dropdown list only when user starts searching values
  3. This supports the containing search

The AutoCompleteTextBox and Test Project Description

  1. Class Diagram for AutoCompleteTextBox

    AutoCompleteTextBox properties description:

    • EnumSource – Source value to the AutoCompleteTextBox. This should be IEnumerable of any object type
    • SelectedText – The selected text/description from the source
    • SelectedItem – The select object from the source
    • DisplayText – The text to display in the AutoCompleteTextBox dropdown list
    • HiglightedText – The highlighted text to display in the AutoCompleteTextBox by default
  2. TestVM Class Diagram

  3. The sample view model TestVM which actually holds the values. Create the IEnumerable property in view mode of type TestNameValue and of type object.
  4. Text property in View model binds to SelectedText property in AutoCompleteTextBox.
  5. Property in View model binds to EnumSource property in AutoCompleteTextBox.
  6. Property in View model binds to SelectedItem property in AutoCompleteTextBox.

Use in Code

How to Consume the Binary?

Add the AutoCompleteTextBox.dll in the test project and write the sample view model as below.

Code Snippet TestNameValue

C#
  public class TestNameValue
  {
      public TestNameValue()
      {
      }
      public TestNameValue(string name, object value)
      {
          this.Name = name;
          this.Value = value;
      }
      public string Name
      {
          get;
          set;
      }
      public object Value
      {
          get;
          set;
      }
      public override string ToString()
      {
          return Name;
      }
  }

  Code snipped Properties in TestVM

      private List<TestNameValue> _values;
      public List<TestNameValue> Values
      {
          get
          {
              return _values;
          }
          set
          {
              _values = value;
              OnPropertyChanged("Values");
          }
      }

      private List<object> _valuesstr;
      public List<object> Valuesstr
      {
          get
          {
              return _valuesstr;
          }
          set
          {
              _valuesstr = value;
              OnPropertyChanged("Valuesstr");
          }
      }

      private string _seltext;
      public string Text
      {
          get
          {
              return _seltext;
          }
          set
          {
              _seltext = value;
              OnPropertyChanged("Text");
          }
      }

      private object _selItem;
      public object SelectedItm
      {
          get
          {
              return _selItem;
          }
          set
          {
              _selItem = value;
              OnPropertyChanged("SelectedItm");
          }
      }

How to Use in XAML?

Add the reference xmlns:ac="clr-namespace:WPfControls;assembly=AutoCompleteTextBox" in XAML.

XML
<ac:AutoCompleteTextBox Width="250" Margin="5" 
                                     EnumSource="{Binding Valuesstr}"
                                     SelectedItem="{Binding SelectedItm,Mode= TwoWay}"
                                     SelectedText="{Binding Text,Mode= TwoWay}"
                                     DisplayText="Name"
                                     HiglightedText="Search"/>
<ac:AutoCompleteTextBox Width="250" Margin="5"  Grid.Row="1"
                                     EnumSource="{Binding Values}"
                                     SelectedItem="{Binding SelectedItm,Mode= TwoWay}"
                                     SelectedText="{Binding Text,Mode= TwoWay}"
                                     DisplayText="Name"
                                     HiglightedText="Search"/>

History

  • 4th April, 2016: Initial release

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Software Developer
India India
I have 10 years of work experience as software developer in C# .net

Comments and Discussions

 
BugThere is a problem when we use ObservableCollection as EnumSource Pin
tikvarova24-Apr-17 23:17
tikvarova24-Apr-17 23:17 
QuestionBroken image links Pin
Jeff Bowman11-Apr-16 15:02
professionalJeff Bowman11-Apr-16 15:02 
BugCode for download is missing Pin
webmaster4424-Apr-16 4:24
webmaster4424-Apr-16 4:24 
The code can't be downloaded, it's missing. Please correct it.
GeneralRe: Code for download is missing Pin
Saravanan Rajamanickam6-Apr-16 18:44
professionalSaravanan Rajamanickam6-Apr-16 18:44 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.