How to cast a System.Windows.Controls.SelectedItemCollection?

社会主义新天地 提交于 2019-11-27 20:06:19

问题


I have a method:

private void DeletePuzzle(object param) 
{
}

param is a System.Windows.Controls.SelectedItemCollection, that I got from a WPF ListView's SelectedItems property.

Somehow, I can't seem to cast it from an object to anything useful. I can't create a System.Windows.Controls.SelectedItemCollection because of its protection level, and param won't cast to IList, ICollection or IEnumerable.

How can I iterate through param's items?


回答1:


Right, got it sorted. I kept trying to cast it like

IList<PuzzleViewModel> collection = (IList<PuzzleViewModel>)param;

Which told me it couldn't convert from SelectedItemCollection to IList...

This is in fact what you need to do.

System.Collections.IList items = (System.Collections.IList)param;
var collection = items.Cast<PuzzleViewModel>();



回答2:


from reflector : -

[Category("Appearance"), Bindable(true), DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]
public IList SelectedItems
{
    get
    {
        return base.SelectedItemsImpl;
    }
}

Selected Items of ListView is an IList, id like to see the calling method.




回答3:


Check The Type: System.Collections.Generic.IList<(Of <(ListViewDataItem>)>)



来源:https://stackoverflow.com/questions/1877949/how-to-cast-a-system-windows-controls-selecteditemcollection

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!