How to cast a System.Windows.Controls.SelectedItemCollection?
27,553
Solution 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>();
Solution 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.
Comments
-
Matt Searles almost 2 years
I have a method:
private void DeletePuzzle(object param) { }
param is a
System.Windows.Controls.SelectedItemCollection
, that I got from a WPFListView
'sSelectedItems
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 toIList
,ICollection
orIEnumerable
.How can I iterate through param's items?
-
user6170001 over 14 yearsI think that's a Web Forms thing, not WPF.
-
Matt Searles over 14 yearsThe calling method was from a RelayCommand (Josh Smiths version). The SelectedItemCollection was getting through ok, but one has to cast it to an IList, not an IList<T>, and then cast that.
-
Hodaya Shalom about 11 yearsThank you, it helped me a lot
-
Peter Duniho about 9 yearsThat's not even legal C# code never mind does the answer provide any useful insight at all.
-
David Bentley over 6 yearsYou can also under certain situations, convert directly to what you need. If you want a
List
or anArray
you can just dovar collection = items.Cast<PuzzleViewModel>().ToArray()
orvar collection = items.Cast<PuzzleViewModel>().ToList()