Export (0) Print
Expand All

CollectionView Class

Represents a view for grouping, sorting, filtering, and navigating a data collection.

Namespace:  System.Windows.Data
Assembly:  PresentationFramework (in PresentationFramework.dll)
XMLNS for XAML: http://schemas.microsoft.com/winfx/2006/xaml/presentation, http://schemas.microsoft.com/netfx/2007/xaml/presentation

public class CollectionView : DispatcherObject, 
	ICollectionView, IEnumerable, INotifyCollectionChanged, INotifyPropertyChanged
See Remarks

The CollectionView type exposes the following members.

  NameDescription
Public methodCollectionViewInitializes a new instance of the CollectionView class that represents a view of the specified collection.
Top

  NameDescription
Protected propertyAllowsCrossThreadChangesGets a value that indicates whether a thread other than the one that created the CollectionView can change the SourceCollection.
Public propertyCanFilterGets a value that indicates whether the view supports filtering.
Public propertyCanGroupGets a value that indicates whether the view supports grouping.
Public propertyCanSortGets a value that indicates whether the view supports sorting.
Public propertyComparerReturns an object that you can use to compare items in the view.
Public propertyCountGets the number of records in the view.
Public propertyCultureGets or sets the culture information to use during sorting.
Public propertyCurrentItemGets the current item in the view.
Public propertyCurrentPositionGets the ordinal position of the CurrentItem within the (optionally sorted and filtered) view.
Public propertyDispatcherGets the Dispatcher this DispatcherObject is associated with. (Inherited from DispatcherObject.)
Public propertyFilterGets or sets a method used to determine if an item is suitable for inclusion in the view.
Public propertyGroupDescriptionsGets a collection of GroupDescription objects that describes how the items in the collection are grouped in the view.
Public propertyGroupsGets a collection of the top-level groups that is constructed based on the GroupDescriptions property.
Public propertyIsCurrentAfterLastGets a value that indicates whether the CurrentItem of the view is beyond the end of the collection.
Public propertyIsCurrentBeforeFirstGets a value that indicates whether the CurrentItem of the view is before the beginning of the collection.
Protected propertyIsCurrentInSyncGets a value that indicates whether the CurrentItem is at the CurrentPosition.
Protected propertyIsDynamicGets a value that indicates whether the underlying collection provides change notifications.
Public propertyIsEmptyGets a value that indicates whether the resulting (filtered) view is empty.
Public propertyIsInUseGets a value that indicates whether any object is subscribing to the events of this CollectionView.
Protected propertyIsRefreshDeferredGets a value that indicates whether there is an outstanding DeferRefresh in use.
Public propertyNeedsRefreshGets a value that indicates whether the view needs to be refreshed.
Public propertyStatic memberNewItemPlaceholderGets the object that is in the collection to represent a new item.
Public propertySortDescriptionsGets a collection of SortDescription structures that describes how the items in the collection are sorted in the view.
Public propertySourceCollectionReturns the underlying unfiltered collection.
Protected propertyUpdatedOutsideDispatcherGets 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 user interface (UI) thread dispatcher.
Top

  NameDescription
Public methodCheckAccessDetermines whether the calling thread has access to this DispatcherObject. (Inherited from DispatcherObject.)
Protected methodClearChangeLog Obsolete. Clears any pending changes from the change log.
Protected methodClearPendingChangesClears unprocessed changed to the collection.
Public methodContainsReturns a value that indicates whether the specified item belongs to the view.
Public methodDeferRefreshEnters a defer cycle that you can use to merge changes to the view and delay automatic refresh.
Public methodDetachFromSourceCollectionRemoves the reference to the underlying collection from the CollectionView.
Public methodEquals(Object)Determines whether the specified object is equal to the current object. (Inherited from Object.)
Protected methodFinalizeAllows an object to try to free resources and perform other cleanup operations before it is reclaimed by garbage collection. (Inherited from Object.)
Protected methodGetEnumeratorReturns an object that you can use to enumerate the items in the view.
Public methodGetHashCodeServes as the default hash function. (Inherited from Object.)
Public methodGetItemAtRetrieves the item at the specified zero-based index in the view.
Public methodGetTypeGets the Type of the current instance. (Inherited from Object.)
Public methodIndexOfReturns the index at which the specified item is located.
Protected methodMemberwiseCloneCreates a shallow copy of the current Object. (Inherited from Object.)
Public methodMoveCurrentToSets the specified item to be the CurrentItem in the view.
Public methodMoveCurrentToFirstSets the first item in the view as the CurrentItem.
Public methodMoveCurrentToLastSets the last item in the view as the CurrentItem.
Public methodMoveCurrentToNextSets the item after the CurrentItem in the view as the CurrentItem.
Public methodMoveCurrentToPositionSets the item at the specified index to be the CurrentItem in the view.
Public methodMoveCurrentToPreviousSets the item before the CurrentItem in the view as the CurrentItem.
Protected methodOKToChangeCurrentReturns a value that indicates whether the view can change which item is the CurrentItem.
Protected methodOnAllowsCrossThreadChangesChangedOccurs when the AllowsCrossThreadChanges property changes.
Protected methodOnBeginChangeLogging Obsolete. Called by the base class to notify the derived class that an INotifyCollectionChanged.CollectionChanged event has been posted to the message queue.
Protected methodOnCollectionChanged(NotifyCollectionChangedEventArgs)Raises the CollectionChanged event.
Protected methodOnCollectionChanged(Object, NotifyCollectionChangedEventArgs)Raises the CollectionChanged event.
Protected methodOnCurrentChangedRaises the CurrentChanged event.
Protected methodOnCurrentChanging()Raises a CurrentChanging event that is not cancelable.
Protected methodOnCurrentChanging(CurrentChangingEventArgs)Raises the CurrentChanging event with the specified arguments.
Protected methodOnPropertyChangedRaises the PropertyChanged event using the specified arguments.
Public methodPassesFilterReturns a value that indicates whether the specified item in the underlying collection belongs to the view.
Protected methodProcessCollectionChangedWhen overridden in a derived class, processes a single change on the UI thread.
Protected methodProcessPendingChangesEnsures that all pending changes to the collection have been committed.
Public methodRefreshRe-creates the view.
Protected methodRefreshOrDeferRefreshes the view or specifies that the view needs to be refreshed when the defer cycle completes.
Protected methodRefreshOverrideRe-creates the view.
Protected methodSetCurrent(Object, Int32)Sets the specified item and index as the values of the CurrentItem and CurrentPosition properties.
Protected methodSetCurrent(Object, Int32, Int32)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.
Public methodToStringReturns a string that represents the current object. (Inherited from Object.)
Public methodVerifyAccessEnforces that the calling thread has access to this DispatcherObject. (Inherited from DispatcherObject.)
Top

  NameDescription
Protected eventCollectionChangedOccurs when the view has changed.
Public eventCurrentChangedOccurs after the CurrentItem has changed.
Public eventCurrentChangingOccurs when the CurrentItem is changing.
Protected eventPropertyChangedOccurs when a property value has changed.
Top

  NameDescription
Public Extension MethodAsParallelEnables parallelization of a query. (Defined by ParallelEnumerable.)
Public Extension MethodAsQueryableConverts an IEnumerable to an IQueryable. (Defined by Queryable.)
Public Extension MethodCast<TResult>Casts the elements of an IEnumerable to the specified type. (Defined by Enumerable.)
Public Extension MethodOfType<TResult>Filters the elements of an IEnumerable based on a specified type. (Defined by Enumerable.)
Top

  NameDescription
Explicit interface implemetationPrivate methodIEnumerable.GetEnumeratorReturns an IEnumerator object that you can use to enumerate the items in the view.
Explicit interface implemetationPrivate eventINotifyCollectionChanged.CollectionChangedOccurs when the view has changed.
Explicit interface implemetationPrivate eventINotifyPropertyChanged.PropertyChangedOccurs when a property value changes.
Top

You should not create objects of this class in your code. 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. 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. 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.

In WPF 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. To get the default view, use the CollectionViewSource.GetDefaultView method. An internal class based on CollectionView is the default view for collections that implement only IEnumerable. ListCollectionView is the default view for collections that implement IList. BindingListCollectionView is the default view for collections that implement IBindingListView or IBindingList.

Alternatively, you can create a view of your collection in Extensible Application Markup Language (XAML) by using the CollectionViewSource class and then bind your control to that view. The CollectionViewSource class is the XAML representation of the CollectionView class. 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.

To set a view in XAML, use the CollectionViewSource class. CollectionViewSource is the XAML representation of the CollectionView class, and it exposes the most commonly used members of the CollectionView class.

Views allow the same data collection to be viewed in different ways, depending on sorting, filtering, or grouping criteria. Every collection has one shared default view, which is used as the actual binding source when a binding specifies a collection as its source. This example shows how to get the default view of a collection.

To create the view, you need an object reference to the collection. This data object can be obtained by referencing your own code-behind object, by getting the data context, by getting a property of the data source, or by getting a property of the binding. This example shows how to get the DataContext of a data object and use it to directly obtain the default collection view for this collection.

myCollectionView = (CollectionView)
    CollectionViewSource.GetDefaultView(rootElem.DataContext);

In this example, the root element is a StackPanel. The DataContext is set to myDataSource, which refers to a data provider that is an ObservableCollection<T> of Order objects.

<StackPanel.DataContext>
  <Binding Source="{StaticResource myDataSource}"/>
</StackPanel.DataContext>

Alternatively, you can instantiate and bind to your own collection view using the CollectionViewSource class. This collection view is only shared by controls that bind to it directly. For an example, see the How to Create a View section in the Data Binding Overview.

For examples of the functionality provided by a collection view, see How to: Sort Data in a View, How to: Filter Data in a View, and How to: Navigate Through the Objects in a Data CollectionView.

More Code

How to: Navigate Through the Objects in a Data CollectionViewViews allow the same data collection to be viewed in different ways, depending on sorting, filtering, or grouping. Views also provide a current record pointer concept and enable moving the pointer. This example shows how to get the current object as well as navigate through the objects in a data collection using the functionality provided in the CollectionView class.
How to: Filter Data in a ViewThis example shows how to filter data in a view.
How to: Sort Data in a ViewThis example describes how to sort data in a view.
How to: Sort and Group Data Using a View in XAMLThis example shows how to create a view of a data collection in Extensible Application Markup Language (XAML). Views allow for the functionalities of grouping, sorting, filtering, and the notion of a current item.

.NET Framework

Supported in: 4.5, 4, 3.5, 3.0

.NET Framework Client Profile

Supported in: 4, 3.5 SP1

Windows 8.1, Windows Server 2012 R2, Windows 8, Windows Server 2012, Windows 7, Windows Vista SP2, Windows Server 2008 (Server Core Role not supported), Windows Server 2008 R2 (Server Core Role supported with SP1 or later; Itanium not supported)

The .NET Framework does not support all versions of every platform. For a list of the supported versions, see .NET Framework System Requirements.

Any public static (Shared in Visual Basic) members of this type are thread safe. Any instance members are not guaranteed to be thread safe.
Show:
© 2014 Microsoft