我将ICollectionView的属性类型绑定到WPF,.NET 4.0中的DataGrid控件上。
我在ICollectionView上使用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视图:
IList<Call> source;
CallsView = CollectionViewSource.GetDefaultView(source);我正在努力解决这个问题:
例如,源数据计数是1000项。我使用过滤器,在DataGrid控件中我只显示了200项。
我想将ICollection当前视图转换为IList<Call>
发布于 2014-01-08 08:11:09
你可以试试:
List<Call> CallsList = CallsView.Cast<Call>().ToList();发布于 2013-01-29 04:35:33
我在Silverlight中遇到了这个问题,但在WPF中也是这样:
IEnumerable<call> calls = collectionViewSource.View.Cast<call>();
发布于 2014-04-09 10:01:47
因为System.Component.ICollectionView没有实现IList,所以不能只调用ToList()。就像Niloo已经回答的一样,您首先需要在集合视图中强制转换项。
您可以使用以下扩展方法:
/// <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();
}用法:
var collectionViewList = MyCollectionViewSource.View.AsList<Call>();https://stackoverflow.com/questions/6859482
复制相似问题