doGmaI
doGmaI

Reputation: 23

Combobox databinding with multiple sources

I recently switched from visual basic 5 to visual basic .NET and I'm trying to learn WPF converting an old program from Windows Forms (written using visual basic 5) to WPF&VB.NET.

In the old program I have a combobox with information loaded from an access database (a simple table with item_id and item_description). Clicking on an item in the combobox causes the id to be saved in a class (which saves its informations in a different table in the database)

Now I'm trying to recreate the same thing in WPF.

I was wondering if it is possibile to have the combobox display the items from the items list binding its itemsource to the items list viewmodel but updating another viewmodel when I change the selected item.

I don't know if it is feasible (and I honestly I tried to find an answer in the web, but looking for "combobox multiple databinding" I end up finding informations that don't help me)

Upvotes: 1

Views: 1393

Answers (1)

David Brunelle
David Brunelle

Reputation: 6440

Not sure if I really understand what you mean, but you can set an itemsource which will be the content of the combo box. It is also possible to bind the selected value of the combo box to another property.

<ComboBox ItemSource="ListItems" SelectedValuePath="item_id" SelectedValue={Binding IdSelected} DisplayMemberPath="item_description"/>

In this exemple, ListItems would be the list that contains the items, SelectedValuePAth will be the name of the property that will be returned by the combobox SelectedValue property. SelectedValue binds to a property of some other object that needs this info. DisplayMemberPath is the property that will be shown to the user.

You can set the itemssource at runtime by using CboItem.Itemssource = listItem, which can be pretty any type of collection (List of, dataset, array, etc.)

Hope that helps.

Upvotes: 1

Related Questions