简体   繁体   中英

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)

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM