具有名称和 ID 的 WPF 组合框
WPF combobox with name and id
我有一个如下所示的组合框:
<ComboBox x:Name="genreComboBox" ItemsSource="{Binding}" SelectionChanged="genreComboBox_SelectionChanged">
<ComboBox.ItemTemplate>
<DataTemplate>
<TextBlock Text="{Binding Name}"/>
</DataTemplate>
</ComboBox.ItemTemplate>
</ComboBox>
我将它绑定到流派列表 List<Genre>
,其中有名称和流派 ID。 Whenever the selection changes, I update the DataContext of a GridView based on that id.所以基本上我需要显示名称,并在 DataContext=someDB.getStuffById(int genreId);
中使用 id
我试过摆弄 getStuffById(genreComboBox.SelectedItem)
和 getStuffById(genreComboBox.SelectedValue)
,设置 DisplayMemeberPath="Name"
和 SelectedValuePath="GenreId"
。大多数时候,显示的是命名。
无论我尝试获取那个 genreId,我总是得到一个 NullReferenceException。
谢谢!
您可以尝试通过原始流派列表调用 getStuffById
。
getStuffById(listofGenres[genreComboBox.SelectedIndex].GenreId);
我有一个如下所示的组合框:
<ComboBox x:Name="genreComboBox" ItemsSource="{Binding}" SelectionChanged="genreComboBox_SelectionChanged">
<ComboBox.ItemTemplate>
<DataTemplate>
<TextBlock Text="{Binding Name}"/>
</DataTemplate>
</ComboBox.ItemTemplate>
</ComboBox>
我将它绑定到流派列表 List<Genre>
,其中有名称和流派 ID。 Whenever the selection changes, I update the DataContext of a GridView based on that id.所以基本上我需要显示名称,并在 DataContext=someDB.getStuffById(int genreId);
我试过摆弄 getStuffById(genreComboBox.SelectedItem)
和 getStuffById(genreComboBox.SelectedValue)
,设置 DisplayMemeberPath="Name"
和 SelectedValuePath="GenreId"
。大多数时候,显示的是命名。
无论我尝试获取那个 genreId,我总是得到一个 NullReferenceException。
谢谢!
您可以尝试通过原始流派列表调用 getStuffById
。
getStuffById(listofGenres[genreComboBox.SelectedIndex].GenreId);