Start New Discussion within our Software Development Community

I have some problems with recieving text from ListBoxItem. My code is below:

ListBox list = (ListBox)this.FindName(e.Parameter.ToString());
ListBoxItem item = (ListBoxItem)list.Items.GetItemAt(0); //****here is exception****
T.Text = item.Content.ToString();

The exception throws:

Unable to cast object of type 'System.String' to type 'System.Windows.Controls.ListBoxItem'.

My ListBox contains numbers, but it does not matter. I just wanna get a handle to first element on list.

You may think it holds numbers, but it is telling you that it holds strings (The System.String part of the error). You are trying to convert this string into a ListBoxItem, which it cannot do.

Based on the next few lines, all you need to do is assign the value to T.Text and skip the attempt to convert it to a ListBoxItem.

This problem is solved. I was adding Strings and reading ListBoxItem types.

This article has been dead for over six months. Start a new discussion instead.