user2236165
user2236165

Reputation: 741

C#: Get object from Combobox populated with Linq

I populate a ComboBox in WPF with this query:

CBKunder.ItemsSource =
    from a in Master.getDC().Kundes
    select a.Kundenavn;

In the selectionChanged method I want to get the object that is selected. I want to do something like this:

kunde = (Kunde)CBKunder.SelectedItem;

But I get an error saying that I can't cast a String to a Kunde, which is the object I thought was alredy in the ComboBox.

What I'm missing here?

Upvotes: 1

Views: 408

Answers (1)

Patrick McCurley
Patrick McCurley

Reputation: 2054

Your select seems to be projecting the a.Kundenavn into the list. Is there a chance that Kundenavn is indeed a string? This would mean your itemsource is a list of Strings, not 'Kunde'

Give

CBKunder.ItemsSource = Master.getDC().Kundes;

a go.

Upvotes: 3

Related Questions