How do I get the value of selected item from listbox

asutorius
asutorius used Ask the Experts™
on
I am trying to get the value (content) of the selected item from a listbox in wpf. This is the code I am using in the codebehind. I get an error that says "Unable to cast object of type 'Users' to type 'System.Windows.Controls.ListBoxItem'". Can you help me get the value?
ListBoxItem lbi = (ListBoxItem)(this.lstUsers.ItemContainerGenerator.ContainerFromIndex(this.lstUsers.SelectedIndex) as ListBoxItem).Content;

Open in new window

Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
ListBoxItem lbi = (ListBoxItem)(this.lstUsers.ItemContainerGenerator.ContainerFromIndex(this.lstUsers.SelectedIndex) as ListBoxItem).Content
Disregard that

Commented:
You need to provide the definition of "lstUsers" for others to help you.
C++ 11 Fundamentals

This course will introduce you to C++ 11 and teach you about syntax fundamentals.

Author

Commented:
It's a listbox with the name of lstUsers which is bound to sql data when the window opens.

Commented:
But why the error message talking sth. about type 'Users' ?

Author

Commented:
If I knew I wouldn't be here asking for help. I am using Entity Framework 4 as the DAL and the object that fills the ListBox is called Users. I don't know if that's related. And if it is related how do you resolve the error message?

Selected item is just

lstUsers.SelectedItem

if it is an object, then you can typecast to that object

Author

Commented:
ewangoya - I am using mvvm pattern so I don't believe your solution will work. Maybe I'm missing something?

Commented:
You are using .NET Framework 4?

Author

Commented:
Yes, I am using .net framework 4.

Commented:
OK. Try this:

            ListBoxItem lbi = this.lstUsers.SelectedItem as ListBoxItem;

Author

Commented:
trestan - i got "Object reference not set to an instance of an object." lbi is null.
Commented:
Actually after I check the document for "ListBoxItem", I noticed that ListBoxItem is created for the backward compatability of VB6. And its construtor only accept string as parameter. I am afraid you have to convert the User object to string to be able to use  "ListBoxItem" type. Why do you have to use this type?

Author

Commented:
I don't understand why I would need to convert the User object to a string. Can you explain?

Also, I have included the xaml and xaml.cs for clarification.

Thanks


xaml

<ListBox Height="226" HorizontalAlignment="Left" Margin="6,30,0,0" Name="lstUsers" VerticalAlignment="Top" Width="144"
                     ItemsSource="{Binding Path=User}" DisplayMemberPath="Name" SelectedValue="{Binding GetRoles}" SelectionChanged="lstUsers_SelectionChanged">
            </ListBox>


xaml.cs
private void lstUsers_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
ListBoxItem lbi = (ListBoxItem)(this.lstUsers.ItemContainerGenerator.ContainerFromIndex(this.lstUsers.SelectedIndex) as ListBoxItem).Content;

MessageBox.Show("You selected " + lbi.ToString());

        }

Here is the solution:
foreach (UsersAndRolesBLL.ViewModel.Users item in lstUsers.Items)
            {
                if (lstUsers.SelectedItems.Contains(item))
                {
                    MessageBox.Show("You selected " + item.Name);
                }
            }

Commented:
So you actually can resolve the problem without using "ListBoxItem". Then tell us why you want to close this question? Where is your respection for others by trying to guess what you really want to do by posting only a single line of code. Just for posting your own "answer" later on?

Author

Commented:
I mean no disrespect. By awarding the points I thought that was the respectful thing to do.

Commented:
Sorry for the chaos I brought up. Seems that I am not familiar with this feature of EE. I remember EE had a "cancel" feature,long time ago? Anyway, just ignore my previous comments.
Thanks for all for your patience.

Commented:
Starting the auto-close procedure on behalf of the question asker.

_alias99
Community Support Moderator

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial