CollectionViewSource.GetDefaultView Method

Returns the default view for the given source.

Namespace:  System.Windows.Data
Assembly:  PresentationFramework (in PresentationFramework.dll)

public static ICollectionView GetDefaultView(
	Object source
)

Parameters

source
Type: System.Object

An object reference to the binding source.

Return Value

Type: System.ComponentModel.ICollectionView
Returns an ICollectionView object that is the default view for the given source collection.

This default view is never affiliated with any CollectionViewSource objects.

All collections have a default CollectionView. WPF always binds to a view rather than a collection. If you bind directly to a collection, WPF actually binds to the default view for that collection. This default view is shared by all bindings to the collection, which causes all direct bindings to the collection to share the sort, filter, group, and current item characteristics of the one default view. Alternatively, you can create a view of your collection in Extensible Application Markup Language (XAML) or code using the CollectionViewSource class, and binding your control to that view. For an example, see How to: Sort and Group Data Using a View in XAML.

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: 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.6, 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.

Was this page helpful?
(1500 characters remaining)
Thank you for your feedback
Show:
© 2014 Microsoft