Files
docs-desktop/dotnet-desktop-guide/framework/wpf/data/how-to-get-the-default-view-of-a-data-collection.md
T
Andy (Steve) De George e9fcf43083 Merge pull request #1135 from dotnet/adegeo-links
Correct .NET Framework links that pointed to old .NET 5 URLs
2021-08-11 12:04:34 -07:00

2.6 KiB

title, ms.date, dev_langs, helpviewer_keywords, ms.assetid
title ms.date dev_langs helpviewer_keywords ms.assetid
How to: Get the Default View of a Data Collection 03/30/2017
csharp
vb
data collections [WPF], creating views of
data binding [WPF], creating views of data collections
b641e96c-c2f6-42ea-9c5d-bac81176ad65

How to: Get the Default View of a Data Collection

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.

Example

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 xref:System.Windows.FrameworkElement.DataContext%2A of a data object and use it to directly obtain the default collection view for this collection.

[!code-csharpCollectionView#2] [!code-vbCollectionView#2]

In this example, the root element is a xref:System.Windows.Controls.StackPanel. The xref:System.Windows.FrameworkElement.DataContext%2A is set to myDataSource, which refers to a data provider that is an xref:System.Collections.ObjectModel.ObservableCollection%601 of Order objects.

[!code-xamlCollectionView#CollectionViewDataContext]

Alternatively, you can instantiate and bind to your own collection view using the xref:System.Windows.Data.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 Sort Data in a View, Filter Data in a View, and Navigate Through the Objects in a Data CollectionView.

See also