Click here to Skip to main content
Licence CPOL
First Posted 14 Dec 2009
Views 17,342
Downloads 1,109
Bookmarked 32 times

WPF Auto-complete Control

By | 14 Dec 2009 | Article
Implementing an auto-complete control in WPF by extending the ComboBox control.

Introduction

There seems to be a lack of support for an auto-complete control in WPF. The closest one to it is the ComboBox which is the base of our implementation for this article.

Background

An auto-complete control is one that allows the user to enter text while querying for a possible selection based on what the user has already entered. The most popular auto-complete implementation deals with querying of a "Starts With" of the current text in the control.

How it Works

Here are some of the properties we care about in the ComboBox:

  • IsEditable- This allows the user to input text into the control.
  • StaysOpenOnEdit - This will force the combobox to stay open when typing.
  • IsTextSearchEnabled - This uses the default "auto-complete" behavior of the ComboBox.

The Magic

By using a combination of the above properties (pretty self-explanatory) and a timer to control the delay of the query, an event which allows the user to attach a new data source, and some styles, we could implement an auto-complete control.

Using the Control

XAML

<Window x:Class="Gui.TestWindow"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:ctr="clr-namespace:Gui.Controls"
      Title="Auto Complete Test" 
      Height="200" Width="300" 
      Loaded="Window_Loaded">
    <StackPanel>
        <StackPanel.Resources>
            <ResourceDictionary 
              Source="/Gui.Controls;component/Styles/AutoComplete.Styles.xaml" />
        </StackPanel.Resources>
        
        <Label>Cities:</Label>
        <ctr:AutoComplete x:Name="autoCities" 
           SelectedValuePath="CityID" DisplayMemberPath="Name" 
           PatternChanged="autoCities_PatternChanged" 
           Style="{StaticResource AutoCompleteComboBox}"
           Delay="500"/> 
        <!-- can also do binding on selected value -->
    </StackPanel>
</Window>

Similar to a combo box, an auto-complete control utilizes the DisplayMemberPath and SelectValuePath properties to bind to a specific data source.

Code-Behind

/// <summary>
/// occurs when the user stops typing after a delayed timespan
/// </summary>
/// <param name="sender"></param>
/// <param name="args"></param>
protected void autoCities_PatternChanged(object sender, 
          Gui.Controls.AutoComplete.AutoCompleteArgs args)
{
    //check
    if (string.IsNullOrEmpty(args.Pattern))
        args.CancelBinding = true;
    else
        args.DataSource = TestWindow.GetCities(args.Pattern);
}

We can utilize the PatternChanged event to subscribe to changes to the the data source. This data source is also equal to a pattern the user has currently entered into the control.

Points of Interest

We utilize the MVVM pattern to create a ViewModel of any entity bound to the data source which contains the HighLight property. Through the use of styles, this highlighted section will be reflected in the dropdown.

History

  • Dec. 14, 2009 - Initial creation.

License

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

About the Author

Yang Yu

Architect
Prognex Corp.
Canada Canada

Member

Love startups, programming, and machine learning.
 
CEO and Founder of FansFave.com
 
LinkedIn: http://www.linkedin.com/in/mryuyang
Twitter: http://twitter.com/mryangyu

Sign Up to vote   Poor Excellent
Add a reason or comment to your vote: x
Votes of 3 or less require a comment

Comments and Discussions

 
You must Sign In to use this message board. (secure sign-in)
 
Search this forum  
 FAQ
    Noise  Layout  Per page   
  Refresh
QuestionPopulate with a value? Pinmembernyland10:30 25 Jan '12  
QuestionConverting to VB Pinmemberchj1248:19 24 Jun '11  
GeneralMy vote of 5 Pinmembernjdnjdnjdnjdnjd22:44 9 May '11  
GeneralMy vote of 1 PinmemberVolcano_8810117:38 8 Nov '10  
GeneralGetting ItemsSource to work Pinmembercjroebuck6:52 15 Dec '09  
GeneralRe: Getting ItemsSource to work PinmemberYang Yu7:29 15 Dec '09  
GeneralThanks a lot! PinmemberRatish Philip23:46 14 Dec '09  
GeneralRe: Thanks a lot! PinmemberYang Yu11:06 15 Dec '09  

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

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

Permalink | Advertise | Privacy | Mobile
Web01 | 2.5.120517.1 | Last Updated 14 Dec 2009
Article Copyright 2009 by Yang Yu
Everything else Copyright © CodeProject, 1999-2012
Terms of Use
Layout: fixed | fluid