在Listbox中与SelectedItems(IList / SelectedObjectCollection)接口的正确方法(view - > viewmodel)

时间:2014-12-22 10:39:48

标签: c# listbox ilist

当与列表框中的一个项目交互时,它很简单:

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

多项目的正确方法是什么?我已经看到了简单地将数据复制到List的示例,但是最好避免复制并进行正确的转换? Resharper告诉我,我可能做错了什么:

enter image description here

建议如下:

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>"

基本上IList / SelectedObjectCollection的用途是什么?

1 个答案:

答案 0 :(得分:1)

  

多项目的正确方法是什么?

SelectedObjectCollection未实施IList<T>。你不能只是施展它。相反,您可以使用Cast方法将所有项目转换为您的类型并将它们放入列表中:

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