Kotkoroid
Kotkoroid

Reputation: 432

ComboBox value returns type instead of the value

The goal is to obtain the current value of a ComboBox that is filled programatically.

In the following C# code snippet, the ItemsSource of a ComboBox is populated with data fetched from an external service:

var categories = new List<CategoryDTO>();

for (var index = 0; index < response.Categories.Count(); index++)
{
    categories.Add(response.Categories.ElementAt(index));
}

CboCategory.DisplayMemberPath = "Name";
CboCategory.SelectedValuePath = "Id";
CboCategory.ItemsSource = categories;

However, when accessing CboCategory.SelectedItem, it returns Interface.Me.DTO.CategoryDTO instead of the actual value that categories was filled with.

Interestingly, the Name field can be accessed in debug mode:

Image capturing the debug mode

Why does it return the class name and how can the actual value be retrieved?

Upvotes: 0

Views: 96

Answers (1)

Matěj Št&#225;gl
Matěj Št&#225;gl

Reputation: 1037

As pointed out by ASh, you need to cast result like this:

((CategoryDTO)CboCategory.SelectedItem).Name 

Upvotes: 2

Related Questions