Find selected WPF Listbox entries
        Posted  
        
            by wonea
        on Stack Overflow
        
        See other posts from Stack Overflow
        
            or by wonea
        
        
        
        Published on 2010-05-06T13:14:24Z
        Indexed on 
            2010/05/06
            13:18 UTC
        
        
        Read the original article
        Hit count: 311
        
I've been trying iterate through the selected items of a listbox in WPF, with the following code;
        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();
        }
However I receive an invalid cast exception;
System.InvalidCastException: Unable to cast object of type 'mylist' to type 'System.Windows.Documents.ListItem'.
Is there a way around this?
© Stack Overflow or respective owner