All that i'm trying to do is to compare, for each value in a listbox, its value with a chosen one and then set the match index as selected. For some reason the exception in the title is raised. I don't understand why though. Code:
foreach(SurfaceListBoxItem n in BackgroundsList.Items)
{
if (n.ToString() == current) BackgroundsList.SelectedItem = n;
}
Thanks!
In WPF, List.Items does not necessarily contain collection of ListBoxItem, instead it only contains data values, and the Item Container of the data is derived, to set value, you must simply set current to selected item.
There is no need to iterate, you can simply do following,
BackgroundsList.SelectedItem = current;