C# 按变量更改选择WPF组合框

C# 按变量更改选择WPF组合框,c#,wpf,combobox,C#,Wpf,Combobox,我使用一个组合框来显示用户可以选择的类别 所有这些项目的id都是SelectedValuePath,因此我可以在用户进行选择时轻松获取这些项目 <ComboBox Grid.Column ="1" Grid.Row="1" HorizontalAlignment="Center" VerticalAlignment="Center" Width="200" Name="cboCategorieen" ItemsSource="{Binding}" DisplayMemberPath="n

我使用一个组合框来显示用户可以选择的类别

所有这些项目的id都是SelectedValuePath,因此我可以在用户进行选择时轻松获取这些项目

<ComboBox Grid.Column ="1" Grid.Row="1" HorizontalAlignment="Center" VerticalAlignment="Center" Width="200" Name="cboCategorieen" ItemsSource="{Binding}" DisplayMemberPath="naam" SelectedValuePath="id" />
总结如下:
如何将组合框的选择更改为与id匹配的组合框?

我建议您遵循MVVM,您的问题的答案仍然是,如果您有这样的ItemsSource

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

在哪里将ItemsSource绑定到组合框?发布代码使用我的Web服务,我会得到所有项目的列表。wswebshopmentals=新的webshopmentals();cboCategorieen.DataContext=wswebshopmentals.getAlleSubcategorien();cboCategorieen.SelectedIndex=0;是否遵循MVVM?否,我仅在窗口打开时获取数据,尝试设置所选值,如cboCategorieen.SelectedValue=selectedCategoryId;
cboCategorieen.SelectedValue = wsWebshopMRentals.FirstOrDefault(x => x.Id == YourID).naam;