Export (0) Print
Expand All

CollectionView Class

Represents a view of a data collection. Views allow for the functionalities of grouping, sorting, filtering, and the concept of a current record pointer.

Namespace: System.Windows.Data
Assembly: PresentationFramework (in presentationframework.dll)
XML Namespace:  http://schemas.microsoft.com/winfx/2006/xaml/presentation

public class CollectionView : DispatcherObject, ICollectionView, IEnumerable, INotifyCollectionChanged, 
	INotifyPropertyChanged
public class CollectionView extends DispatcherObject implements ICollectionView, IEnumerable, 
	INotifyCollectionChanged, INotifyPropertyChanged
public class CollectionView extends DispatcherObject implements ICollectionView, IEnumerable, 
	INotifyCollectionChanged, INotifyPropertyChanged
 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. 

You can think of a collection view as the layer on top of the 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 raised by the CollectionChanged event are propagated to the views.

Because views do not change the underlying source collections, each source collection can have multiple views associated with it. For example, you may have a collection of Task objects. With the use of views, you can display that same data in different ways. For example, on the left side of your page you may want to show tasks sorted by priority, and on the right side, grouped by area.

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, you use the GetDefaultView method. CollectionView is the default view object for collections that only implement IEnumerable. For all collections implementing IList, the ListCollectionView object is the default view object. The BindingListCollectionView is the collection view class used for collections that implement IBindingListView and IBindingList.

Alternatively, you can create a view of your collection in Extensible Application Markup Language (XAML) using the CollectionViewSource class and then bind your control to that view. CollectionViewSource class is the Extensible Application Markup Language (XAML) representation of the CollectionView class. For an example, see How to: Create a View of a Data Collection in XAML.

For more information, see "Binding to Collections" in the Data Binding Overview.

Views allow the same data collection to be viewed in different ways, depending on sorting or filtering. Views also provide a current record pointer concept and enable moving the pointer. This example shows how to create a view object.

To create the view, you need an object reference to the original data object. 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 associated CollectionView. The GetDefaultView method is used to obtain the default collection view of the root element.

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 of Order objects.

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

Alternatively, you can instantiate a view object directly using the CollectionViewSource class. 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 Sort Data in a Data Collection View, Filter Data in a Data Collection View, and Navigate Through the Objects in a Data Collection View.

More Code

How to: Navigate Through the Objects in a Data CollectionView

Views 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 View

This example shows how to filter data in a view.

How to: Sort Data in View

This example describes how to sort data in a view.

How to: Sort and Group Data Using a View in XAML

This 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.

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

Windows 98, Windows Server 2000 SP4, Windows CE, Windows Millennium Edition, Windows Mobile for Pocket PC, Windows Mobile for Smartphone, Windows Server 2003, Windows XP Media Center Edition, Windows XP Professional x64 Edition, Windows XP SP2, Windows XP Starter Edition

The Microsoft .NET Framework 3.0 is supported on Windows Vista, Microsoft Windows XP SP2, and Windows Server 2003 SP1.

.NET Framework

Supported in: 3.0

Community Additions

ADD
Show:
© 2014 Microsoft