查找选定的WPF列表框条目

won*_*nea 1 c# wpf .net-3.5

我一直在尝试使用以下代码遍历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();
        }
Run Code Online (Sandbox Code Playgroud)

但是我收到了无效的强制转换异常;

System.InvalidCastException:无法将类型为"mylist"的对象强制转换为"System.Windows.Documents.ListItem".

有没有解决的办法?