简体   繁体   中英

Get a value from ComboBox in WPF

I would like to ask how can I get a value from ComboBox which is currently displayed.

Here is a code that fills my ComboBox with values:

List<CategoryDTO> categories = new List<CategoryDTO>();
for (Int32 index = 0; index < response.Categories.Count(); index++)
{
     categories.Add(response.Categories.ElementAt(index));
}
CboCategory.DisplayMemberPath = "Name";
CboCategory.SelectedValuePath = "Id";
CboCategory.ItemsSource = categories;

These are outputs it is giving me:

CboCategory.SelectedValue = c4617c70-fa21-48c3-81da-3ddb647941b0 CboCategory.SelectedItem = Interface.Me.DTO.CategoryDTO

In a debug mode I see I can access the Name but I can't managed to get it.

My question is, how can I get CboCategory.SelectedItem.Name value?

Thank you all!

正如ASh指出的那样,您需要像这样强制转换结果:

((CategoryDTO)CboCategory.SelectedItem).Name 

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM