Reputation:
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>
Upvotes: 16
Views: 18875
Reputation: 357
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<T> 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<T> of the specified type.</param>
/// <returns>A System.Collections.Generic.List<T> 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>();
Upvotes: 1
Reputation: 1215
You can try:
List<Call> CallsList = CallsView.Cast<Call>().ToList();
Upvotes: 28
Reputation: 1611
I just ran into this problem in Silverlight, but its the same in WPF:
IEnumerable<call> calls = collectionViewSource.View.Cast<call>();
Upvotes: 1
Reputation: 4561
Can you just use an extension method to convert:
IList<Call> source = collection.ToList();
Upvotes: 0