Collection​View Class

Definition

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

public class CollectionView : System.Windows.Threading.DispatcherObject, System.Collections.IEnumerable, System.Collections.Specialized.INotifyCollectionChanged, System.ComponentModel.ICollectionView, System.ComponentModel.INotifyPropertyChanged
Inheritance
CollectionView
Derived
Implements

Inherited Members

System.Object

System.Windows.Threading.DispatcherObject

Remarks

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 System.Windows.Data.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.

XAML Text Usage

See Remarks

Constructors

Collection​View(​IEnumerable)

Initializes a new instance of the CollectionView class that represents a view of the specified collection.

Properties

Allows​Cross​Thread​Changes

Gets a value that indicates whether a thread other than the one that created the CollectionView can change the SourceCollection.

Can​Filter

Gets a value that indicates whether the view supports filtering.

Can​Group

Gets a value that indicates whether the view supports grouping.

Can​Sort

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.

Current​Item

Gets the current item in the view.

Current​Position

Gets the ordinal position of the CurrentItem within the (optionally sorted and filtered) view.

Filter

Gets or sets a method used to determine if an item is suitable for inclusion in the view.

Group​Descriptions

Gets a collection of GroupDescription objects that describes how the items in the collection are grouped in the view.

Groups

Gets a collection of the top-level groups that is constructed based on the GroupDescriptions property.

Is​Current​After​Last

Gets a value that indicates whether the CurrentItem of the view is beyond the end of the collection.

Is​Current​Before​First

Gets a value that indicates whether the CurrentItem of the view is before the beginning of the collection.

Is​Current​In​Sync

Gets a value that indicates whether the CurrentItem is at the CurrentPosition.

Is​Dynamic

Gets a value that indicates whether the underlying collection provides change notifications.

Is​Empty

Gets a value that indicates whether the resulting (filtered) view is empty.

Is​In​Use

Gets a value that indicates whether any object is subscribing to the events of this CollectionView.

Is​Refresh​Deferred

Gets a value that indicates whether there is an outstanding DeferRefresh() in use.

Needs​Refresh

Gets a value that indicates whether the view needs to be refreshed.

New​Item​Placeholder

Gets the object that is in the collection to represent a new item.

Sort​Descriptions

Gets a collection of SortDescription structures that describes how the items in the collection are sorted in the view.

Source​Collection

Returns the underlying unfiltered collection.

Updated​Outside​Dispatcher

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 user interface (UI) thread dispatcher.

Methods

Clear​Change​Log()

Clears any pending changes from the change log.

Clear​Pending​Changes()

Clears unprocessed changed to the collection.

Contains(​Object)

Returns a value that indicates whether the specified item belongs to the view.

Defer​Refresh()

Enters a defer cycle that you can use to merge changes to the view and delay automatic refresh.

Detach​From​Source​Collection()

Removes the reference to the underlying collection from the CollectionView.

Get​Enumerator()

Returns an object that you can use to enumerate the items in the view.

Get​Item​At(​Int32)

Retrieves the item at the specified zero-based index in the view.

Index​Of(​Object)

Returns the index at which the specified item is located.

Move​Current​To(​Object)

Sets the specified item to be the CurrentItem in the view.

Move​Current​To​First()

Sets the first item in the view as the CurrentItem.

Move​Current​To​Last()

Sets the last item in the view as the CurrentItem.

Move​Current​To​Next()

Sets the item after the CurrentItem in the view as the CurrentItem.

Move​Current​To​Position(​Int32)

Sets the item at the specified index to be the CurrentItem in the view.

Move​Current​To​Previous()

Sets the item before the CurrentItem in the view as the CurrentItem.

O​KTo​Change​Current()

Returns a value that indicates whether the view can change which item is the CurrentItem.

On​Allows​Cross​Thread​Changes​Changed()

Occurs when the AllowsCrossThreadChanges property changes.

On​Begin​Change​Logging(​Notify​Collection​Changed​Event​Args)

Called by the base class to notify the derived class that an CollectionChanged event has been posted to the message queue.

On​Collection​Changed(​Notify​Collection​Changed​Event​Args)

Raises the CollectionChanged event.

On​Collection​Changed(​Object, ​Notify​Collection​Changed​Event​Args)

Raises the CollectionChanged event.

On​Current​Changed()

Raises the CurrentChanged event.

On​Current​Changing(​Current​Changing​Event​Args)

Raises the CurrentChanging event with the specified arguments.

On​Current​Changing()

Raises a CurrentChanging event that is not cancelable.

On​Property​Changed(​Property​Changed​Event​Args)

Raises the PropertyChanged event using the specified arguments.

Passes​Filter(​Object)

Returns a value that indicates whether the specified item in the underlying collection belongs to the view.

Process​Collection​Changed(​Notify​Collection​Changed​Event​Args)

When overridden in a derived class, processes a single change on the UI thread.

Process​Pending​Changes()

Ensures that all pending changes to the collection have been committed.

Refresh()

Re-creates the view.

Refresh​Or​Defer()

Refreshes the view or specifies that the view needs to be refreshed when the defer cycle completes.

Refresh​Override()

Re-creates the view.

Set​Current(​Object, ​Int32)

Sets the specified item and index as the values of the CurrentItem and CurrentPosition properties.

Set​Current(​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.

Events

Collection​Changed

Occurs when the view has changed.

Current​Changed

Occurs after the CurrentItem has changed.

Current​Changing

Occurs when the CurrentItem is changing.

Property​Changed

Occurs when a property value has changed.

Explicit Interface Implementations

I​Enumerable.​Get​Enumerator()

Returns an IEnumerator object that you can use to enumerate the items in the view.

Extension Methods

Cast<TResult>(IEnumerable)
OfType<TResult>(IEnumerable)
AsParallel(IEnumerable)
AsQueryable(IEnumerable)