简体   繁体   English

如何从C#中的对象获取元素?

[英]How to get elements from an object in C#?

I am using the AutoCompleteBox in WPF, I populate the suggestions with a List that consists of four fields. 我在WPF中使用AutoCompleteBox,并使用包含四个字段的列表填充建议。 When the user selects an item and I reach my eventHandler, i can see that 当用户选择一个项目并到达eventHandler时,我可以看到

MyAutoCompleteBox.SelectedItem

is an object that has my four values, if i hover this text in the debugger i can see the four values listed, however i don't know how to access these values in the code. 是具有四个值的对象,如果我在调试器中悬停此文本,则可以看到列出的四个值,但是我不知道如何在代码中访问这些值。

I tried 我试过了

List<Codes> selected = MyAutoCompleteBox.SelectedItem as List<Codes>;

where Codes is my List. 代码是我的清单。 selected returns as null and empty every time. selected每次都返回null和空。 Is there a way to get to these values? 有没有办法获得这些价值? Thanks! 谢谢!

如果要列出用作AutoCompleteBox的后备集合的项目列表,请尝试... AutoCompleteBox.ItemsSource

这意味着您无法将MyAutoCompleteBox.SelectedItem转换为列表。

Can you try: 你能试一下吗:

Codes selected = MyAutoCompleteBox.SelectedItem as Codes;

or 要么

Codes[] selected = MyAutoCompleteBox.SelectedItem as Codes[];

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

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