Matt
Matt

Reputation: 1923

Change selection WPF combobox by variable

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

enter image description here

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?

Upvotes: 0

Views: 325

Answers (1)

Sajeetharan
Sajeetharan

Reputation: 222522

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;

Upvotes: 1

Related Questions