Convert ICollectionView to List<T>
Asked Answered
I

4

16

I am binding property type of ICollectionView on DataGrid controls in WPF, .NET 4.0.

I use Filter on ICollectionView.

    public ICollectionView CallsView
    {
        get
        {
            return _callsView;
        }
        set
        {
            _callsView = value;
            NotifyOfPropertyChange(() => CallsView);
        }
    }

    private void FilterCalls()
    {
        if (CallsView != null)
        {
            CallsView.Filter = new Predicate<object>(FilterOut);
            CallsView.Refresh();
        }
    }

    private bool FilterOut(object item)
    {
       //..
    }

Init ICollection view:

IList<Call> source;
CallsView = CollectionViewSource.GetDefaultView(source);

I am trying to solve this problem:

For example source data count is 1000 items. I use filter, in DataGrid control I show only 200 items.

I would like convert ICollection current view to IList<Call>

Issus answered 28/7, 2011 at 13:9 Comment(1)
Refer to #188413Quathlamba
C
28

You can try:

List<Call> CallsList = CallsView.Cast<Call>().ToList();
Cyna answered 8/1, 2014 at 8:11 Comment(2)
Works perfectly when working with CollectionViewSource for getting the list of currently visible items on the view.Libelee
As you can see here (#4016430) you can use List<Call> CallsList = CallsView.OfType<Call>().ToList(); alternativly.Staphylo
O
1

I just ran into this problem in Silverlight, but its the same in WPF:

IEnumerable<call> calls = collectionViewSource.View.Cast<call>();

Ointment answered 29/1, 2013 at 4:35 Comment(0)
G
1

Because System.Component.ICollectionView doesn't implement IList, you can't just call ToList(). Like Niloo already answered, you first need to cast the items in the collection view.

You could use the following extension method:

/// <summary>
/// Casts a System.ComponentModel.ICollectionView of as a System.Collections.Generic.List&lt;T&gt; of the specified type.
/// </summary>
/// <typeparam name="TResult">The type to cast the elements of <paramref name="source"/> to.</typeparam>
/// <param name="source">The System.ComponentModel.ICollectionView that needs to be casted to a System.Collections.Generic.List&lt;T&gt; of the specified type.</param>
/// <returns>A System.Collections.Generic.List&lt;T&gt; that contains each element of the <paramref name="source"/>
/// sequence cast to the specified type.</returns>
/// <exception cref="ArgumentNullException"><paramref name="source"/> is <c>null</c>.</exception>
/// <exception cref="InvalidCastException">An element in the sequence cannot be cast to the type <typeparamref name="TResult"/>.</exception>
[SuppressMessage("Microsoft.Design", "CA1002:DoNotExposeGenericLists", Justification = "Method is provided for convenience.")]
public static List<TResult> AsList<TResult>(this ICollectionView source)
{
    return source.Cast<TResult>().ToList();
}

Usage:

var collectionViewList = MyCollectionViewSource.View.AsList<Call>();
Gemmiparous answered 9/4, 2014 at 10:1 Comment(0)
D
0

Can you just use an extension method to convert:

IList<Call> source = collection.ToList();
Deledda answered 1/3, 2012 at 11:18 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.