【发布时间】:2011-02-16 09:40:21
【问题描述】:
我一直在尝试使用以下代码遍历 WPF 中列表框的选定项;
try
{
for (int i = 0; i < mylistbox.SelectedItems.Count; i++)
{
ListItem li = (ListItem)mylistbox.SelectedItems[i];
string listitemcontents_str = li.ToString();
}
}
catch(Exception e)
{
// Error appears here
string error = e.ToString();
}
但是我收到了一个无效的强制转换异常;
System.InvalidCastException:无法将“mylist”类型的对象转换为“System.Windows.Documents.ListItem”类型。
有没有办法解决这个问题?
【问题讨论】: