Silverlite: Why your AutoCompleteBox may not be working

I downloaded the new Silverlight Toolkit from CodePlex and used the AutoCompleteBox control on my recent Silverlight Datagrid app.  It worked great, that is, after I struggled to figure out why it wasn’t.

My problem was that I was replacing a ComboBox control with an AutoCompleteBox and was trying to bind a collection to the AutoCompleteBox the same way I was binding it to the ComboBox.

This is how I was binding a collection to my custom ComboBox in a PreparingCellForEdit event.

MyComboBox employeeList = (MyComboBox)dg.CurrentColumn.GetCellContent(e.Row);
employeeList.ItemsSource = employees;

See the problem with attempting to set the AutoCompleteBox’s ItemsSource to employees, a collection of type JobEmployee?  Right.  AutoCompleteBox binds to an IEnumerable like a STRING list.  Think AutoCompleteBox = List of Strings.

AutoCompleteBox emps = (AutoCompleteBox)dg.CurrentColumn.GetCellContent(e.Row);
var empString = (from c in employees
         select c.EmployeeName);

emps.ItemsSource = empString;
emps.Focus();

Thanks to this Tim Heuer post on using AutoCompleteBox with custom types that showed me the light.

Article written by

A long time developer, I was an early adopter of Linux in the mid-90's for a few years until I entered corporate environments and worked with Microsoft technologies like ASP, then .NET. In 2008 I released Sueetie, an Online Community Platform built in .NET. In late 2012 I returned to my Linux roots and locked in on Java development. Much of my work is available on GitHub.