2011-07-28 16 views
8

Estoy vinculando el tipo de propiedad de ICollectionView en controles DataGrid en WPF, .NET 4.0.Convertir ICollectionView en la lista <T>

Yo uso Filter en 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) 
    { 
     //.. 
    } 

vista Init ICollection:

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

estoy tratando de resolver este problema:

Por ejemplo recuento de datos de origen es de 1000 artículos. Yo uso filtro, en el control DataGrid, solo muestro 200 elementos.

me gustaría convertir ICollection vista actual a IList<Call>

+0

Consulte http://stackoverflow.com/questions/187913/c-fastest-convert-from-collection -to-listt – nthpixel

Respuesta

0

Puede usted sólo tiene que utilizar un método de extensión para convertir:

IList<Call> source = collection.ToList(); 
1

Me acabo de encontrar con este problema en Silverlight, pero es la misma en WPF:

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

13

Usted puede tratar de:

+0

Funciona perfectamente cuando se trabaja con 'CollectionViewSource' para obtener la lista de elementos actualmente visibles en la vista. – qJake

1

Como System.Component.ICollectionView no implementa IList, no puede simplemente llamar a ToList(). Al igual que Niloo ya respondió, primero debe lanzar los elementos en la vista de colección.

Se puede utilizar el siguiente método de extensión:

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

Uso:

var collectionViewList = MyCollectionViewSource.View.AsList<Call>(); 
Cuestiones relacionadas