Correct way to interface with SelectedItems (IList / SelectedObjectCollection) in Listbox (view -> viewmodel)

766 views Asked by At

When interfacing with one item in a listbox, its simple enough to go:

  myClass citem = (myClass)myListBop.SelectedItem.
  viewModel.doSomethingWithItem(cItem)

Whats the correct way for multiple items? I have seen examples of simply copying the data to a List, but surely it would be best to avoid the copy and go for the correct cast? Resharper kindly informs me that I might be doing something wrong:

enter image description here

Where the suggestion is:

Suspicious case: there is no type in the solution which is inherited from both "Sytem.Windows.Forms.ListBox.SelectedObjectCollection" and "System,Collections.Generic.IList<myClass>"

Basically what is the intended usage of IList / SelectedObjectCollection?

1

There are 1 answers

2
Selman Genç On BEST ANSWER

Whats the correct way for multiple items?

SelectedObjectCollection does not implement IList<T>. you can't just cast it. Instead you can use Cast method to cast all items to your type and put them in a list:

myListBop.SelectedItems.Cast<myClass>().ToList();