ISerialized .Net, C#, Scrum and agile software development

3Jun/101

Using the AutoCompleteBox in the WPF Toolkit

In this blog post I will look into the AutoCompleteBox in the WPF Toolkit, added in the february release of the WPF Toolkit. The AutoCompleteBox works, just the way I had expected it, but it actually adds a couple of more features than what I would have expected. Only one dll is needed to use the AutoCompleteBox:

System.Windows.Controls.Input.Toolkit

I will not describe all the features in details, only describe the basics of what you need to know to use it! To add a basic AutoCompleteBox, I just add the following line to my XAML:


<Controls:AutoCompleteBox Name="autocompleteBox" />


Once I have the user control in place, I can set ItemsSource and hence have the "predefined" values available:

using System.Collections.Generic;
using System.Windows;

namespace Autocomplete
{
    /// <summary>
    /// Interaction logic for Window1.xaml
    /// </summary>
    public partial class Window1 : Window
    {
        public Window1()
        {
            InitializeComponent();
            var mylist = new List<string>
                             {
                                 "Macedonia",
                                 "Madagascar",
                                 "Malawi",
                                 "Malaysia",
                                 "Maldives",
                                 "Mali",
                                 "Malta",
                                 "Marshall Islands",
                                 "Mauritania",
                                 "Mauritius",
                                 "Mexico",
                                 "Micronesia",
                                 "Moldova",
                                 "Monaco",
                                 "Mongolia",
                                 "Montenegro",
                                 "Morocco",
                                 "Mozambique",
                                 "Myanmar"
                             };
            autocompleteBox.ItemsSource = mylist;
        }
    }
}

Running my test project I see the following  behaviour:

There are basically just one more thing you need to notice about the AutoCompleteBox namely the FilterMode property. On my AutoCompleteBox I can set the filter mode to configure how the auto-complete values are picked. The following list is available at MSDN:

Member name Description
None Specifies that no filter is used. All items are returned.
StartsWith Specifies a culture-sensitive, case-insensitive filter where the returned items start with the specified text. The filter uses the String.StartsWith method, specifying StringComparer.CurrentCultureIgnoreCase as the string comparison criteria.
StartsWithCaseSensitive Specifies a culture-sensitive, case-sensitive filter where the returned items start with the specified text. The filter uses the String.StartsWith method, specifying StringComparer.CurrentCulture as the string comparison criteria.
StartsWithOrdinal Specifies an ordinal, case-insensitive filter where the returned items start with the specified text. The filter uses the String.StartsWith method, specifying StringComparer.OrdinalIgnoreCase as the string comparison criteria.
StartsWithOrdinalCaseSensitive Specifies an ordinal, case-sensitive filter where the returned items start with the specified text. The filter uses the String.StartsWith method, specifying StringComparer.Ordinal as the string comparison criteria.
Contains Specifies a culture-sensitive, case-insensitive filter where the returned items contain the specified text.
ContainsCaseSensitive Specifies a culture-sensitive, case-sensitive filter where the returned items contain the specified text.
ContainsOrdinal Specifies an ordinal, case-insensitive filter where the returned items contain the specified text.
ContainsOrdinalCaseSensitive Specifies an ordinal, case-sensitive filter where the returned items contain the specified text.
Equals Specifies a culture-sensitive, case-insensitive filter where the returned items equal the specified text. The filter uses the String.Equals method, specifying StringComparer.CurrentCultureIgnoreCase as the search comparison criteria.
EqualsCaseSensitive Specifies a culture-sensitive, case-sensitive filter where the returned items equal the specified text. The filter uses the String.Equals method, specifying StringComparer.CurrentCulture as the string comparison criteria.
EqualsOrdinal Specifies an ordinal, case-insensitive filter where the returned items equal the specified text. The filter uses the String.Equals method, specifying StringComparer.OrdinalIgnoreCase as the string comparison criteria.
EqualsOrdinalCaseSensitive Specifies an ordinal, case-sensitive filter where the returned items equal the specified text. The filter uses the String.Equals method, specifying StringComparer.Ordinal as the string comparison criteria.
Custom Specifies that a custom filter is used. This mode is used when the AutoCompleteBox.TextFilter or AutoCompleteBox.ItemFilter properties are set.

Thats basically all you need to know! In a real-life case I would of course add the ItemSource in the XAML binding it to a corresponding property in my ViewModel, but for simplicity I decided not to add it here just to keep my example as easy and clean as possible!

Posted by Pål Eie

Comments (1) Trackbacks (0)
  1. Have you ever thought about writing an ebook or guest authoring on other
    sites? I have a blog centered on the same ideas
    you discuss and would love to have you share some stories/information.
    I know my visitors would enjoy your work. If you are even remotely
    interested, feel free to shoot me an email. Appreciate it!


Leave a comment

No trackbacks yet.