Export (0) Print
Expand All

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
)
public static ICollectionView GetDefaultView (
	Object source
)
public static function GetDefaultView (
	source : Object
) : ICollectionView
You cannot use methods in XAML.

Parameters

source

An object reference to the binding source.

Return Value

Returns an ICollectionView object that is the default view for the given source.

This default view is never affiliated with any CollectionViewSource objects.

All collections have a default CollectionView. 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.

Using this method is one way of creating a view of your collection. Alternatively, you can create a view of your collection in Extensible Application Markup Language (XAML) using the CollectionViewSource class directly 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.

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

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