views:

596

answers:

1

Please tell me the basic and quick way to bind a Collection(List,etc) to a combobox and handle the selection changed event and get the selected item.

+1  A: 

This is quite easy. You can do this with XAML+Code or just code. I won't type out a complete solution as I feel you'll benefit more from completing that part yourself. I've assumed here that you already have some XAML declaring a combo box, so I've just shown some code (in C# as you didn't state what language you were using), just know that the event handler could easily be assigned via XAML instead.

this.combo.SelectionChanged +=
    new SelectionChangedEventHandler(comboProjects_SelectionChanged);

this.combo.ItemsSource = myCollectionOfItems;

This sets up an event handler for the selection changing and also binds the combo to a collection, which it uses to source its items (hence the name, ItemsSource).

Then, in the SelectionChanged event handler, you can get the SelectedItem property to find out what is selected.

Note that there are some caveats with binding in the ComboBox, so you may find these links useful:

Jeff Yates
Thanks.. this will also help me... the part where I am really stuck is getting the items displayed in the Combobox.. using the {Binding...} and DisplayMemberPath, etc things... I only get the business object type name in the combo drop down...