×

First time here?

You are looking at the most recent posts. You may also want to check out older archives. Please leave a comment, ask a question and consider subscribing to the latest posts via RSS or email. Thank you for visiting!

The other day I wrote a simple little Silverlight application using a DataGrid to help navigate the TechEd DVD contents.  My code was admittedly quick and dirty.  I loaded up some data, and based on some events re-filtered and re-bound that data.  After thinking about it I am not sure why I didn’t just use some existing controls to help me do that work.  I found that AutoCompleteBox from the new Silverlight Toolkit would do this for me.

One thing that the AutoCompleteBox does quickly is provide filtering for simple string data.  But what about custom types?  My data in the TechEd application has a List<TechEdSession> where TechEdSession is respresented as such:

   1: public class TechEdSession
   2: {
   3:     public string DiscNumber { get; set; }
   4:     public string SessionCode { get; set; }
   5:     public string Track { get; set; }
   6:     public string SessionTitle { get; set; }
   7:     public string Speakers { get; set; }
   8:     public string TechEdConf { get; set; }
   9: }

If you wire up the AutoCompleteBox.ItemsSource to my List<> nothing will happen.  Why?  Well the control doesn’t know how to understand the data exactly.  So we have to help it.  It knows the data is there but doesn’t know what to return based on the filter.

This is simply done using the ItemFilter property on the control.  We can easily provide a Lambda expression to help the control understand the filter.  After setting the ItemsSource property to the results of my LINQ query, I add the expression to help the control understand the data:

   1: SessionFilter.ItemsSource = sessions;
   2: SessionFilter.ItemFilter = (search, item) =>
   3:     {
   4:         TechEdSession session = item as TechEdSession;
   5:         if (session != null)
   6:         {
   7:             string filter = search.ToLower();
   8:             return ((session.SessionCode.ToLower().Contains(filter) || 
   9:                 session.SessionTitle.ToLower().Contains(filter) || 
  10:                 session.Speakers.ToLower().Contains(filter)) &&
  11:                 session.TechEdConf.ToLower().Contains(((ComboBoxItem)ConfSelector.SelectedItem).Content.ToString().ToLower()));
  12:         }
  13:  
  14:         return false;
  15:     };

That’s it.  I’ve now defined the filter for the control so as the user types data it does the appropriate filtering logic and displays the results.  I’ve chosen to still display the results as a DataGrid for easy reading in this application, but could have also used an ItemTemplate if I desired.  The ItemFilter expects a search predicate to be passed to it.

So the resulting application has the same capabilities, but a lot less code for me to write.  Here’s the ending result:

So if you need to implement AutoCompleteBox and have it search/filter your custom types, be sure to supply your own ItemFilter to help the control know where to look!


This work is licensed under a Creative Commons Attribution By license.


11/3/2008 1:41 PM | # re: Using Silverlight AutoCompleteBox on custom types
Tim,
I haven't had time to come up with such a nice, succinct post on custom items.

One thing I’d like to add is that, in your example, the value displayed in the text box when you move around in the list is ‘TechEdNADVD.TechEdSession’.

This is expected, since that is your object name. However, to provide a more meaningful text value, you have two options:

1. If you own the object, override the ToString() to return the name, or other important value. We’ll use that.

2. You can create a value converter that takes your TechEdSession instance and returns the string representation. You can set the Converter dependency property of the AutoCompleteBox to this converter either in XAML or code.

-Jeff
11/3/2008 1:54 PM | # re: Using Silverlight AutoCompleteBox on custom types
Jeff: Ooops, thanks for the reminder. Admittedly I'm using AutoComplete box for a filter on a datagrid. Actually selecting an item here is meaningless, but I ignored at least making it less meaningless :-). What I'd love so see is the same logic applied to a target UIElement. So i can say the target of autocomplete is an existing DataGrid. I've updated my streaming app though to override the ToString to give some meaning :-).
11/3/2008 9:16 PM | # re: Using Silverlight AutoCompleteBox on custom types
Have you tried this using VB or using a WebService? I have added an Overrides ToString function to my class but the Autocompletebox still uses the the class name.

Also, using a regular combobox, when I select an item from a rich ItemTemplate with mutliple controls it stays dispayed the same way after I select it. The same functionality would be much nicer in the AutoCompleteBox. It would be even nicer if I could define a "SelectedTemplate" in the same way I can define an "ItemTemplate". That way I could have different behavior for searching and after selection.
11/4/2008 5:48 AM | # re: Using Silverlight AutoCompleteBox on custom types
Jeremy: If you want to send me your sample project I can take a look. As to the suggestion for the the SelectedTemplate, you should add that suggestion to their forums and/or issue list.
11/6/2008 5:19 PM | # re: Using Silverlight AutoCompleteBox on custom types
Turns out my issue is that I am using a WebService as my datasource which will not pass anything but Read/Write Properties across the wire. Basically, you can't override the ToString() function of a custom class if you have passed that class over a web service.
11/7/2008 4:25 AM | # re: Using Silverlight AutoCompleteBox on custom types
hi Jeremy Craven, all the classes auto-generated by visual studio (when new service reference is created) are partial class, so you can create a partial class of your class and override ToString().
11/7/2008 3:32 PM | # re: Using Silverlight AutoCompleteBox on custom types
Thanks. It's a pain to have to add the additional functionality every time I refresh my web service reference but it works so thank you.
4/16/2009 1:08 PM | # re: Using Silverlight AutoCompleteBox on custom types
Hi Tim,
I would like to select multiple rows in the data grid and the textbox be filled with a comma separated string of the selected items. Any idea how to do this?
4/23/2009 7:46 PM | # re: Using Silverlight AutoCompleteBox on custom types
When using the MVVM pattern, specifically the Composite Application Library, can the ItemFilter be set using Databinding?
4/24/2009 7:31 AM | # re: Using Silverlight AutoCompleteBox on custom types
To answer my own question, yes, ItemFilter can be set using Databinding. Set the property in the ModelView as follows:

private AutoCompleteSearchPredicate<object> myItemFilter;
public AutoCompleteSearchPredicate<object> MyItemFilter
{
get { return myItemFilter; }
set
{
myItemFilter = value;
InvokePropertyChanged(new PropertyChangedEventArgs("MyItemFilter"));
}
}
7/1/2009 7:43 PM | # re: Using Silverlight AutoCompleteBox on custom types
How did you use the data grid as a template for the drop-down piece of the autocomplete box?

<inputToolkit:AutoCompleBox.ItemTemplate>

is giving me an error.



Can you post the whole source code? (or possibly just the xaml?)

Thanks!
7/12/2009 3:47 PM | # re: Using Silverlight AutoCompleteBox on custom types
I'm using an AutoCompleteBox with a webserive and ma having the same issue as Jeremy Craven. I just cant quite figure out how to overide ToString() in order for it to display the value in the autocomplete box. And because of this is seems to also call on SelectionChanged the sub twice once with new selection and once agin with the first item in the web service results. Anyone got any ideas or some across this before
8/30/2009 1:12 AM | # Source Code
You can please public download (direct) link ?
8/30/2009 3:17 AM | # re: Using Silverlight AutoCompleteBox on custom types
How did you show the results on DataGrid using AutoCompleteBox ? Can you please public download url (Source Code) ?
9/3/2009 7:42 AM | # re: Using Silverlight AutoCompleteBox on custom types
anyone ? please ?
10/4/2009 9:08 AM | # re: Using Silverlight AutoCompleteBox on custom types
Please provide the source code, I also need to show the data grid, but i can not figure out how to do it?
11/10/2009 1:42 AM | # re: Using Silverlight AutoCompleteBox on custom types
Hi

Is there a way to have a Button and a list of items displayed in DropDown list in Autocomplete text box.
2/23/2010 6:36 AM | # re: Using Silverlight AutoCompleteBox on custom types
Hello Tim,

Can you plz share the source code and we are unable to see the result on the site.
10/18/2012 1:20 PM | # re: Using Silverlight AutoCompleteBox on custom types
I tried using silverlight before and it works good. But as I formatted my pc I didn't get an access of it again. I don't know what's the problem. - Theodore Stroukoff

 
Please add 3 and 2 and type the answer here:

DISCLAIMER:

The opinions/content expressed on this blog are provided "ASIS" with no warranties and are my own personal opinions/content (unless otherwise noted) and do not represent my employer's view in any way.