Autocomplete in Silverlight

I’ve written a simple class to allow adding autocomplete to a TextBox. An example of how to use it is:

In the XAML file:

<TextBox x:Name="uiText" Width="100" Height="30"
    manas:Autocomplete.Suggest="DoSuggest" />

In the class file:

// The texts we want to suggest
private string[] options = new string[]
{
    "Al", "Amiko", "Angla", "Anglujo", "Ankaux", "Antaux", "Atomo", "Auxto",
    "Bebo", "Bela", "Birdo",
}

// The method we have to implement to offer suggestions
public void DoSuggest(string text, SuggestCallback callback)
{
    // Don't suggest if there's no text
    if (text.Length == 0)
    {
        callback(null);
        return;
    }

    var result = new List();

    // See which options have as a prefix the text entered by the user
    foreach (var option in options)
    {
        if (option.StartsWith(text, StringComparison.InvariantCultureIgnoreCase))
        {
            result.Add(new Suggestion() { DisplayString = option,
                                                      ReplaceString = option });
        }
    }

    callback(result.ToArray());
}

As in a previous post, I use the technique to specify an action to happen in the XAML, and it’s implementation in the class file, just like an event handler.

Of course, instead of using an hardcoded options array, these could be requested in the DoSuggest method to a web service, or requested from another class.

The suggestions are shown in an unstyled ListBox, but it should be easy to style, and even to improve the code to support icons in the suggestions, or any other control.

For this to work, the RootVisual of your application must be a Canvas, since otherwise you can’t place arbitrary floating elements on top of it.

Here’s the full code in case someone finds it useful.

(+) Show Code

5 Responses to Autocomplete in Silverlight

  1. Ben Griswold says:

    Hi Ary,

    Thanks for offering up your AutoComplete solution to my question on StackOverflow.com. I’ve reviewed this post as well as the previous post and, though the code looks great, I’m unable to get the functionality to work. I receive the following exception, per the AttachEvent method, at runtime: Can’t find handler ‘DoSuggest’ (maybe it’s not public?). I’m sure I’m overlooking something. Does anything come to mind? Can you offer even more help?

    Thanks,
    Ben

  2. Ary Borenszweig Ary Borenszweig says:

    Thanks for the feedback. I already corrected the article. Now it should work.

  3. Pingback: I Am Not A Control Developer by JohnnyCoder

  4. vipul says:

    I am unable to get EventSupport class while running the application with Silverlight 2.0 Can you please suggest on the same.

  5. ldereu says:

    Thanks, works like a charm

Leave a Reply

Your email address will not be published. Required fields are marked *

*


*

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>