Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
1.5k views
in Technique[技术] by (71.8m points)

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

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?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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>();

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...