Frank Vermeulen
Frank Vermeulen

Reputation: 59

WPF combobox with name and id

I've got a ComboBox which looks like this:

    <ComboBox x:Name="genreComboBox" ItemsSource="{Binding}" SelectionChanged="genreComboBox_SelectionChanged">
        <ComboBox.ItemTemplate>
            <DataTemplate>
                <TextBlock Text="{Binding Name}"/>
            </DataTemplate>
        </ComboBox.ItemTemplate>
    </ComboBox>

I'm binding it to a list of Genres List<Genre>, which have a Name and a genreId. Whenever the selection changes, I update the DataContext of a GridView based on that id. So basically I need to display the name, and use the id in a DataContext=someDB.getStuffById(int genreId);

I've tried messing around with getStuffById(genreComboBox.SelectedItem) and getStuffById(genreComboBox.SelectedValue), setting DisplayMemeberPath="Name" and SelectedValuePath="GenreId". Most of the time, the named get displayed.

Whatever I try to get that genreId out, I always get a NullReferenceException.

Thank you!

Upvotes: 0

Views: 1319

Answers (1)

Saifeddine M
Saifeddine M

Reputation: 483

You may try calling getStuffById by the orignal list of genres.

getStuffById(listofGenres[genreComboBox.SelectedIndex].GenreId);

Upvotes: 1

Related Questions