Reputation: 432
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:
Why does it return the class name and how can the actual value be retrieved?
Upvotes: 0
Views: 96
Reputation: 1037
As pointed out by ASh, you need to cast result like this:
((CategoryDTO)CboCategory.SelectedItem).Name
Upvotes: 2