How to get elements from an object in C#?
-
30-09-2019 - |
Question
I am using the AutoCompleteBox in WPF, I populate the suggestions with a List that consists of four fields. When the user selects an item and I reach my eventHandler, i can see that
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. Is there a way to get to these values? Thanks!
Solution
Can you try:
Codes selected = MyAutoCompleteBox.SelectedItem as Codes;
or
Codes[] selected = MyAutoCompleteBox.SelectedItem as Codes[];
OTHER TIPS
If you want the listing of items used as the backing collection for the AutoCompleteBox
try...AutoCompleteBox.ItemsSource.
It means that you cannot convert whatever MyAutoCompleteBox.SelectedItem is to a List.
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow