简体   繁体   English

从WPF中的ComboBox获取值

[英]Get a value from ComboBox in WPF

I would like to ask how can I get a value from ComboBox which is currently displayed. 我想问一下如何从当前显示的ComboBox中获取一个值。

Here is a code that fills my ComboBox with values: 这是一个用值填充我的ComboBox的代码:

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 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? 我的问题是,如何获取CboCategory.SelectedItem.Name值?

Thank you all! 谢谢你们!

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

((CategoryDTO)CboCategory.SelectedItem).Name 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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