Highlighting AutoCompleteBox improvements thanks to Silverlight 3

About half a year ago, I blogged a nice “highlighting AutoCompleteBox” post that allowed you to use the Silverlight Toolkit’s AutoCompleteBox control to give your users an experience just like that of Internet Explorer 8, Firefox, and other nice interfaces.

Now, with Silverlight 3, the sample app has dropped in size, removing the need for 160 lines of “hack” code to enable the scenario.

This functionality was done using a helper control that I built, called HighlightingTextBlock, that has two primary dependency properties: the Text to display, and the HighlightText (subset) that should be highlighted visually.


If you have the Silverlight 3 Beta developer runtime, click here to view the sample app.

In Silverlight 2, without UI-to-UI binding (no ElementName), unfortunately this blog post included a little intense code to actually walk the visual tree, find all the HighlightingTextBlock instances, and update their values, whenever the searching changes. This was because a direct binding could not be made to the parent AutoCompleteBox control’s “SearchText” property, which is a string value that is set to whatever the current searching text is.

Thanks to the Silverlight 3 Beta’s great support for ElementName binding, I was able to update the sample to be super easy.

Using this, I can use the following ItemTemplate setting for any AutoCompleteBox to enable this very nice highlighting behavior.

<input:AutoCompleteBox
    x:Name="HighlightingAutoCompleteBox" IsTextCompletionEnabled="False" SearchMode="Contains">
    <input:AutoCompleteBox.ItemTemplate>
        <DataTemplate>
            <highlightingTextBlock:HighlightingTextBlock
                HighlightText="{Binding SearchText, ElementName=HighlightingAutoCompleteBox}"
                Text="{Binding}"
                HighlightBrush="#FF44A0FF" />
        </DataTemplate>
    </input:AutoCompleteBox.ItemTemplate>
</input:AutoCompleteBox>

I think this would work with Relative Source as well, but haven’t spent the time to try yet. Hope this helps! I can’t wait for the final release of Silverlight 3, this beta is a really nice set of improvements.

Comments

  1. Ben
    March 27th, 2009 | 12:32 am

    Hello Jeff,

    Can you recommend good tools for Unit testing Silverlight ?

  2. Arnoud van Bers
    March 31st, 2009 | 1:37 am

    Great!
    Too bad element binding needs a name and cannot be an instance..

  3. March 31st, 2009 | 9:41 pm

    @Ben,
    I favor the Silverlight Unit Test Framework that ships inside the Silverlight Toolkit, but that’s probably because I designed, built, and shipped it single-handedly :-) .

  4. chadbr
    April 21st, 2009 | 6:08 am

    The live sample is broken — I just get error on page @ the bottom of IE8

  5. Niranjan
    April 29th, 2009 | 11:12 pm

    Is there a Unit Testing Framework for Silverlight 3. Or is there any possible way of converting the UT for Silverlight2 and make it work for Silverlight3 applications. Please leave me a suggestion how i can test Silverlight 3 applications?

  6. April 29th, 2009 | 11:29 pm

    @Niranjan,
    The Silverlight 2 unit test framework is completely compatible with Silverlight 3 and works great.

  7. Niranjan
    April 30th, 2009 | 1:02 am

    Thanks Jeff. That helped me a lot as i have to start working with that. I jus wanna know if i can try testing Silverlight3 UT Framework in Microsoft Visual Web Developer 2008 Express Edition or should i have to use Visual Studi 2008.?If i can use the Web Developer Edition should i have to make any alterations?
    Also can you tell me whether i can use this Silverlight UT to test WCFs in the same application.
    Please do pardon me if these are basic questions. I am jus getting started with Silverlight.