views:

736

answers:

2

Hi,

I need to implement an editable combobox where users can select existing values from the data/tables. It needs to be editable because users can also add new rows to the table by entering new values in the editable combobox, so I put an AutoCompleteBox control into my page but I can't find any sample on how to implement such feature. It should display something like Employee Name in the editable dropdown while having the SelectedValue property to contain the Employee ID.

Any help will be very much appreciated.

Cheers!

+2  A: 

Hi Leon

You will need to bind your autocompletebox's ItemsSource to your "lookup" collection. You can use ValueMemberBinding to resolve the textual input to look for, ie if you have a list of people, bind this to Model.Name like this {Binding Name} to find people by name.

As far as the drop down items, you could use templating to display the items the way you like. Heres a good tut on the subject, you want to style the ItemTemplate. following from the example you would make a datatemplate in xaml within the ItemsTemplate element add a Textblock and bind its Text property to Name like {Binding Name}.

Here a nice example where a autocompletebox is styled like a combobox. You could extend that to look for "enter" on TextChanged and check to see if the item is contained in the ItemsSource. If not it could push the new value to the server (if you are into MVVM, you could raise a command on you ViewModel that would delegate the addition to the server and update the Items).

almog.ori
A: 

Here's another example that extends the AutoCompleteBox to be used as a type-ahead ComboBox. It can handle foreign keys / lookup ids using DPs and can be used in MVVM scenarios. AutoComplete ComboBox for Silverlight

Syed Mehroz Alam