Raja
Raja

Reputation:

Silverlight Combobox data binding : Basic and quick way?

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.

Upvotes: 1

Views: 1095

Answers (1)

Jeff Yates
Jeff Yates

Reputation: 62377

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:

Upvotes: 1

Related Questions