none
Casting System.Windows.Controls.SelectedItemCollection

    Question

  • Hi,

    A method of mine is being passed a System.Windows.Controls.SelectedItemCollection (a ListViews SelectedItems field) as an object... which I am trying to cast back to... well, anything I can iterate through really. Apparently I can't create an instance of System.Windows.Controls.SelectedItemCollection because of it's protection level, and it won't cast to IList, IEnumerable or ICollection. So.. how can I get at the items?

    private void DeletePuzzle(object param)
    {
        // Attempt to cast param
        var collection = (IEnumerable<PuzzleViewModel>param); // won't cast

        // Delete models...
    }

    Warm regards,
    Matt
    Thursday, December 10, 2009 12:32 AM

Answers

  • ListView's SelectedItems property returns an IList.

    Assuming you're using .NET 3.5, you can just do this:

    System.Collections.IList items = (System.Collections.IList) param;
    var collection = items.Cast<PuzzleViewModel>();
    • Marked as answer by Matt Searles Thursday, December 10, 2009 2:28 AM
    Thursday, December 10, 2009 2:14 AM

All replies

  • ListView's SelectedItems property returns an IList.

    Assuming you're using .NET 3.5, you can just do this:

    System.Collections.IList items = (System.Collections.IList) param;
    var collection = items.Cast<PuzzleViewModel>();
    • Marked as answer by Matt Searles Thursday, December 10, 2009 2:28 AM
    Thursday, December 10, 2009 2:14 AM
  • Well I'll be damned.  Cheers Adam. 
    Thursday, December 10, 2009 2:27 AM