CollectionView 类

定义

表示用于对数据集合进行分组、排序、筛选和导航的视图。Represents a view for grouping, sorting, filtering, and navigating a data collection.

public ref class CollectionView : System::Windows::Threading::DispatcherObject, System::Collections::IEnumerable, System::Collections::Specialized::INotifyCollectionChanged, System::ComponentModel::ICollectionView, System::ComponentModel::INotifyPropertyChanged
public class CollectionView : System.Windows.Threading.DispatcherObject, System.Collections.IEnumerable, System.Collections.Specialized.INotifyCollectionChanged, System.ComponentModel.ICollectionView, System.ComponentModel.INotifyPropertyChanged
type CollectionView = class
    inherit DispatcherObject
    interface ICollectionView
    interface IEnumerable
    interface INotifyCollectionChanged
    interface INotifyPropertyChanged
Public Class CollectionView
Inherits DispatcherObject
Implements ICollectionView, IEnumerable, INotifyCollectionChanged, INotifyPropertyChanged
继承
CollectionView
派生
实现

注解

不应在代码中创建此类的对象。You should not create objects of this class in your code. IEnumerable要创建仅实现的集合的集合视图,请创建一个CollectionViewSource对象, Source将集合添加到属性View中,并从属性获取集合视图。To create a collection view for a collection that only implements IEnumerable, create a CollectionViewSource object, add your collection to the Source property, and get the collection view from the View property.

您可以将集合视图看作绑定源集合的一个层,它允许您根据排序、筛选和组查询来导航并显示集合,所有这些操作都无需处理基础源集合本身。You can think of a collection view as a layer on top of a binding source collection that allows you to navigate and display the collection based on sort, filter, and group queries, all without having to manipulate the underlying source collection itself. 如果源集合实现了INotifyCollectionChanged接口,则CollectionChanged引发事件的更改将传播到视图。If the source collection implements the INotifyCollectionChanged interface, the changes that raise the CollectionChanged event are propagated to the views.

由于视图不会更改基础源集合,因此,源集合可以有多个关联的视图。Because a view does not change the underlying source collection, a source collection can have multiple views associated with it. 通过使用视图,可以通过不同方式显示相同的数据。By using views, you can display the same data in different ways. 例如,您可以对Task对象集合使用两个视图,以显示按优先级排序的任务(在页面的某个部分上按优先级排序),并在页面的另一部分上按区域分组。For example, you can use two views on a collection of Task objects to show tasks sorted by priority on one part of the page and grouped by area on another part of the page.

WPFWPF应用程序中,所有集合都具有关联的默认集合视图。In WPFWPF applications, all collections have an associated default collection view. 绑定引擎始终通过关联视图访问集合,而不是直接使用集合。Rather than working with the collection directly, the binding engine always accesses the collection through the associated view. 若要获取默认视图,请使用CollectionViewSource.GetDefaultView方法。To get the default view, use the CollectionViewSource.GetDefaultView method. 基于CollectionView的内部类是仅IEnumerable实现的集合的默认视图。An internal class based on CollectionView is the default view for collections that implement only IEnumerable. ListCollectionView是实现IList的集合的默认视图。ListCollectionView is the default view for collections that implement IList. BindingListCollectionView是实现IBindingListViewIBindingList的集合的默认视图。BindingListCollectionView is the default view for collections that implement IBindingListView or IBindingList.

或者,您可以可扩展应用程序标记语言 (XAML)Extensible Application Markup Language (XAML) CollectionViewSource使用类在中创建集合的视图,然后将控件绑定到该视图。Alternatively, you can create a view of your collection in 可扩展应用程序标记语言 (XAML)Extensible Application Markup Language (XAML) by using the CollectionViewSource class and then bind your control to that view. 类是类CollectionView的表示形式。 XAMLXAML CollectionViewSourceThe CollectionViewSource class is the XAMLXAML representation of the CollectionView class. 有关示例,请参见 如何:在 XAML中使用视图对数据进行排序和分组。For an example, see How to: Sort and Group Data Using a View in XAML.

有关详细信息,请参阅数据绑定概述中的 "绑定到集合"。For more information, see "Binding to Collections" in Data Binding Overview.

若要在中XAMLXAML设置视图,请CollectionViewSource使用类。To set a view in XAMLXAML, use the CollectionViewSource class. CollectionViewSourceCollectionView类的CollectionView表示形式,它公开了类中最常使用的成员。 XAMLXAMLCollectionViewSource is the XAMLXAML representation of the CollectionView class, and it exposes the most commonly used members of the CollectionView class.

构造函数

CollectionView(IEnumerable)

初始化表示指定集合视图的 CollectionView 类的新实例。Initializes a new instance of the CollectionView class that represents a view of the specified collection.

属性

AllowsCrossThreadChanges

获取指示创建的 CollectionView 线程能更改 SourceCollection 的值。Gets a value that indicates whether a thread other than the one that created the CollectionView can change the SourceCollection.

CanFilter

获取一个值,该值指示视图是否支持筛选。Gets a value that indicates whether the view supports filtering.

CanGroup

获取一个值,该值指示视图是否支持分组。Gets a value that indicates whether the view supports grouping.

CanSort

获取一个值,该值指示视图是否支持排序。Gets a value that indicates whether the view supports sorting.

Comparer

返回可用于比较视图中的项的对象。Returns an object that you can use to compare items in the view.

Count

获取视图中的记录数。Gets the number of records in the view.

Culture

获取或设置排序期间使用的区域性信息。Gets or sets the culture information to use during sorting.

CurrentItem

获取视图中的当前项。Gets the current item in the view.

CurrentPosition

获取 CurrentItem 在(可能经过排序和筛选)视图中的序号位置。Gets the ordinal position of the CurrentItem within the (optionally sorted and filtered) view.

Dispatcher

获取与此 Dispatcher 关联的 DispatcherObjectGets the Dispatcher this DispatcherObject is associated with.

(继承自 DispatcherObject)
Filter

获取或设置用于确定某个项是否适合包含在视图中的方法。Gets or sets a method used to determine if an item is suitable for inclusion in the view.

GroupDescriptions

获取 GroupDescription 对象的集合,该集合描述集合中的项在视图中的分组方式。Gets a collection of GroupDescription objects that describes how the items in the collection are grouped in the view.

Groups

获取顶级组的集合,该集合根据 GroupDescriptions 属性构造而成。Gets a collection of the top-level groups that is constructed based on the GroupDescriptions property.

IsCurrentAfterLast

获取一个值,该值指示视图的 CurrentItem 是否超出集合的末尾。Gets a value that indicates whether the CurrentItem of the view is beyond the end of the collection.

IsCurrentBeforeFirst

获取一个值,该值指示视图的 CurrentItem 是否在集合开头之前。Gets a value that indicates whether the CurrentItem of the view is before the beginning of the collection.

IsCurrentInSync

获取一个值,该值指示 CurrentItem 是否位于 CurrentPositionGets a value that indicates whether the CurrentItem is at the CurrentPosition.

IsDynamic

获取一个值,该值指示基础集合是否提供更改通知。Gets a value that indicates whether the underlying collection provides change notifications.

IsEmpty

获取一个值,该值指示最终的(已筛选)视图是否为空。Gets a value that indicates whether the resulting (filtered) view is empty.

IsInUse

获取指示所有对象是否支持此 CollectionView 事件的值。Gets a value that indicates whether any object is subscribing to the events of this CollectionView.

IsRefreshDeferred

获取一个值,该值指示是否有某个未完成的 DeferRefresh() 正在运行。Gets a value that indicates whether there is an outstanding DeferRefresh() in use.

NeedsRefresh

获取一个值,该值指示视图是否需要刷新。Gets a value that indicates whether the view needs to be refreshed.

NewItemPlaceholder

获取集合中表示新项的对象。Gets the object that is in the collection to represent a new item.

SortDescriptions

获取 SortDescription 结构的集合,该集合描述集合中的项在视图中的排序方式。Gets a collection of SortDescription structures that describes how the items in the collection are sorted in the view.

SourceCollection

返回未筛选的基础集合。Returns the underlying unfiltered collection.

UpdatedOutsideDispatcher

获取一个值,该值指示是否必须更新更改日志,因为在没有先进入用户界面 (UI)user interface (UI) 线程调度程序的情况下,已在其他线程上收到 CollectionChanged 通知。Gets a value that indicates whether it has been necessary to update the change log because a CollectionChanged notification has been received on a different thread without first entering the 用户界面 (UI)user interface (UI) thread dispatcher.

方法

CheckAccess()

确定调用线程是否可以访问此 DispatcherObjectDetermines whether the calling thread has access to this DispatcherObject.

(继承自 DispatcherObject)
ClearChangeLog()

从更改日志中清除所有挂起的更改。Clears any pending changes from the change log.

ClearPendingChanges()

清除未处理更改为集合。Clears unprocessed changed to the collection.

Contains(Object)

返回一个值,该值指示指定项是否属于视图。Returns a value that indicates whether the specified item belongs to the view.

DeferRefresh()

进入延迟循环,该循环可用于将更改合并到视图并延迟自动刷新。Enters a defer cycle that you can use to merge changes to the view and delay automatic refresh.

DetachFromSourceCollection()

CollectionView 中移除对基础集合的引用。Removes the reference to the underlying collection from the CollectionView.

Equals(Object)

确定指定的对象是否等于当前对象。Determines whether the specified object is equal to the current object.

(继承自 Object)
GetEnumerator()

返回可用于枚举视图中的项的对象。Returns an object that you can use to enumerate the items in the view.

GetHashCode()

用作默认哈希函数。Serves as the default hash function.

(继承自 Object)
GetItemAt(Int32)

检索视图中位于从零开始的指定索引处的项。Retrieves the item at the specified zero-based index in the view.

GetType()

获取当前实例的 TypeGets the Type of the current instance.

(继承自 Object)
IndexOf(Object)

返回指定项所在的索引位置。Returns the index at which the specified item is located.

MemberwiseClone()

创建当前 Object 的浅表副本。Creates a shallow copy of the current Object.

(继承自 Object)
MoveCurrentTo(Object)

将指定项设置为视图中的 CurrentItemSets the specified item to be the CurrentItem in the view.

MoveCurrentToFirst()

将视图中的第一项设置为 CurrentItemSets the first item in the view as the CurrentItem.

MoveCurrentToLast()

将视图中的最后一项设置为 CurrentItemSets the last item in the view as the CurrentItem.

MoveCurrentToNext()

将视图中位于 CurrentItem 之后的项设置为 CurrentItemSets the item after the CurrentItem in the view as the CurrentItem.

MoveCurrentToPosition(Int32)

将位于指定索引位置的项设置为视图中的 CurrentItemSets the item at the specified index to be the CurrentItem in the view.

MoveCurrentToPrevious()

将视图中位于 CurrentItem 之前的项设置为 CurrentItemSets the item before the CurrentItem in the view as the CurrentItem.

OKToChangeCurrent()

返回一个值,该值指示视图是否可以更改作为 CurrentItem 的项。Returns a value that indicates whether the view can change which item is the CurrentItem.

OnAllowsCrossThreadChangesChanged()

AllowsCrossThreadChanges 属性更改时发生。Occurs when the AllowsCrossThreadChanges property changes.

OnBeginChangeLogging(NotifyCollectionChangedEventArgs)

由基类调用,以通知派生类 CollectionChanged 事件已发送到消息队列。Called by the base class to notify the derived class that an CollectionChanged event has been posted to the message queue.

OnCollectionChanged(NotifyCollectionChangedEventArgs)

引发 CollectionChanged 事件。Raises the CollectionChanged event.

OnCollectionChanged(Object, NotifyCollectionChangedEventArgs)

引发 CollectionChanged 事件。Raises the CollectionChanged event.

OnCurrentChanged()

引发 CurrentChanged 事件。Raises the CurrentChanged event.

OnCurrentChanging()

引发一个不可取消的 CurrentChanging 事件。Raises a CurrentChanging event that is not cancelable.

OnCurrentChanging(CurrentChangingEventArgs)

用指定的参数引发 CurrentChanging 事件。Raises the CurrentChanging event with the specified arguments.

OnPropertyChanged(PropertyChangedEventArgs)

用指定的参数引发 PropertyChanged 事件。Raises the PropertyChanged event using the specified arguments.

PassesFilter(Object)

返回一个值,该值指示基础集合中的指定项是否属于视图。Returns a value that indicates whether the specified item in the underlying collection belongs to the view.

ProcessCollectionChanged(NotifyCollectionChangedEventArgs)

在派生类中重写时,处理 UIUI 线程上的单个更改。When overridden in a derived class, processes a single change on the UIUI thread.

ProcessPendingChanges()

确保此集合的所有待定更改都已被提交。Ensures that all pending changes to the collection have been committed.

Refresh()

重新创建视图。Re-creates the view.

RefreshOrDefer()

刷新视图或指定视图在延迟循环完成后需要刷新。Refreshes the view or specifies that the view needs to be refreshed when the defer cycle completes.

RefreshOverride()

重新创建视图。Re-creates the view.

SetCurrent(Object, Int32)

将指定项和索引设置为 CurrentItemCurrentPosition 属性的值。Sets the specified item and index as the values of the CurrentItem and CurrentPosition properties.

SetCurrent(Object, Int32, Int32)

将指定项和索引设置为 CurrentItemCurrentPosition 属性的值。Sets the specified item and index as the values of the CurrentItem and CurrentPosition properties. 可以从派生类的构造函数中调用此方法。This method can be called from a constructor of a derived class.

ToString()

返回一个表示当前对象的 string。Returns a string that represents the current object.

(继承自 Object)
VerifyAccess()

强制调用线程具有此 DispatcherObject 的访问权限。Enforces that the calling thread has access to this DispatcherObject.

(继承自 DispatcherObject)

事件

CollectionChanged

在视图更改时发生。Occurs when the view has changed.

CurrentChanged

CurrentItem 更改后发生。Occurs after the CurrentItem has changed.

CurrentChanging

CurrentItem 正在更改时发生。Occurs when the CurrentItem is changing.

PropertyChanged

在更改属性值时发生。Occurs when a property value has changed.

显式界面实现

IEnumerable.GetEnumerator()

返回可用于枚举视图中的项的 IEnumerator 对象。Returns an IEnumerator object that you can use to enumerate the items in the view.

INotifyCollectionChanged.CollectionChanged

在视图更改时发生。Occurs when the view has changed.

INotifyPropertyChanged.PropertyChanged

在属性值更改时发生。Occurs when a property value changes.

扩展方法

Cast<TResult>(IEnumerable)

IEnumerable 的元素强制转换为指定的类型。Casts the elements of an IEnumerable to the specified type.

OfType<TResult>(IEnumerable)

根据指定类型筛选 IEnumerable 的元素。Filters the elements of an IEnumerable based on a specified type.

AsParallel(IEnumerable)

启用查询的并行化。Enables parallelization of a query.

AsQueryable(IEnumerable)

IEnumerable 转换为 IQueryableConverts an IEnumerable to an IQueryable.

适用于