简体   繁体   中英

Change selection WPF combobox by variable

I'm using a combobox to show categories a user can choose.

在此处输入图片说明

All those items have the id as the SelectedValuePath so I can easly get those when the user makes a selection.

<ComboBox Grid.Column ="1" Grid.Row="1" HorizontalAlignment="Center" VerticalAlignment="Center" Width="200" Name="cboCategorieen" ItemsSource="{Binding}" DisplayMemberPath="naam" SelectedValuePath="id" />

But how can I change the selection of the combobox when I have the id of the chosen selection (SelectedValuePath)? This code doesn't do anything and just keeps selecting the first one.

cboCategorieen.SelectedValuePath = Convert.ToString(artikelWijzigen.categorie);

To conclude: How can I change the selection of the combobox to the one matching the id?

I would suggest you to follow MVVM, Still the answer for your question would be Say if you have a ItemsSource like this,

ObservableCollection<YourComboBoxClass> wsWebshopMRentals;

You can set the selectedItem of combobox like this,

cboCategorieen.SelectedValue = wsWebshopMRentals.FirstOrDefault(x => x.Id == YourID).naam;

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