【问题标题】:Correct way to interface with SelectedItems (IList / SelectedObjectCollection) in Listbox (view -> viewmodel)在列表框中(视图-> 视图模型)与 SelectedItems(IList / SelectedObjectCollection)交互的正确方法
【发布时间】:2014-12-22 10:39:47
【问题描述】:

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

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

多个项目的正确方法是什么?我已经看到了简单地将数据复制到列表的示例,但是肯定最好避免复制并进行正确的演员表吗? Resharper 好心地告诉我,我可能做错了什么:

建议在哪里:

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 的预期用途是什么?

【问题讨论】:

  • ListBox.SelectedObjectCollection和IList&lt;MyClass&gt;之间没有关系,它们都以IList为基类

标签: c# listbox ilist


【解决方案1】:

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

SelectedObjectCollection 没有实现IList&lt;T&gt;。你不能只是施放它。相反,您可以使用Cast 方法将所有项目转换为您的类型并将它们放在一个列表中:

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

【讨论】:

  • 我需要“tolist”吗?我可以简单地使用结果吗? "IEnumerable"
  • @chrispepper1989 在您遍历结果之前不会执行 Cast。 ToList 进行迭代。如果您不想立即枚举结果,可以删除 ToList
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2014-05-16
  • 1970-01-01
  • 2012-12-30
  • 1970-01-01
  • 1970-01-01
  • 2011-08-19
  • 1970-01-01
相关资源
最近更新 更多