CollectionViewSource.Source Property

Microsoft Silverlight will reach end of support after October 2021. Learn more.

Gets or sets the collection object from which to create this view.

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

Syntax

'Declaration
Public Property Source As Object
public Object Source { get; set; }
<collectionViewSource>
  <collectionViewSource.Source>
    <Object .../>
  </collectionViewSource.Source>
</collectionViewSource>
<collectionViewSource Source="myCollection"/>

XAML Values

Term

Definition

myCollection

An existing collection. To refer to an existing collection, use the StaticResource Markup Extension.

Property Value

Type: System.Object
The collection object from which to create this view. The default is nulla null reference (Nothing in Visual Basic).

Exceptions

Exception Condition
ArgumentException

The specified value when setting this property is not nulla null reference (Nothing in Visual Basic) or an IEnumerable implementation.

-or-

The specified value when setting this property is an ICollectionView implementation.

InvalidOperationException

The specified value implements ICollectionViewFactory but its CreateView method returns an ICollectionView with one or more of the following inconsistencies:

Remarks

When you set this property, a view is generated automatically unless the source collection implements ICollectionViewFactory. Otherwise, the view is retrieved through the CreateView method.

You can retrieve the view through the View property.

Version Notes

Silverlight 3 does not support the ICollectionViewFactory interface.

Examples

The following code example demonstrates the use of this property in XAML to apply sorting to a data source. The data is also defined in XAML to enable you to see the effects of the CollectionViewSource in the Silverlight Designer in Visual Studio 2010.

This example uses the Customer and CustomerCollection classes defined in Walkthrough: Using Sample Data in the Silverlight Designer. The walkthrough also describes how to use design-time attributes to display sample data in the Silverlight Designer.

<UserControl x:Class="CollectionViewSourceExample.MainPage"
  xmlns="https://schemas.microsoft.com/winfx/2006/xaml/presentation"
  xmlns:x="https://schemas.microsoft.com/winfx/2006/xaml"
  xmlns:d="https://schemas.microsoft.com/expression/blend/2008"
  xmlns:mc="https://schemas.openxmlformats.org/markup-compatibility/2006"
  xmlns:local="clr-namespace:DesignDataDemo"
  xmlns:compMod="clr-namespace:System.ComponentModel;assembly=System.Windows"
  xmlns:sdk="https://schemas.microsoft.com/winfx/2006/xaml/presentation/sdk"
  mc:Ignorable="d" d:DesignHeight="300" d:DesignWidth="400" >

  <UserControl.Resources>

    <local:CustomerCollection x:Key="customerSampleData" >
      <local:Customer FirstName="Syed" LastName="Abbas" Age="23" 
        CustomerID="E7181DC6-3F9E-45A4-A5F7-AC0B119D1FD8" />
      <local:Customer FirstName="Brenda" LastName="Diaz" Age="55" 
        CustomerID="BB638D72-8B72-495A-B0F9-79F37964BBAE" />
      <local:Customer FirstName="Lori" LastName="Kane" Age="17" 
        CustomerID="B168D811-5548-4D28-8171-318F9A4D7219" />
    </local:CustomerCollection>

    <CollectionViewSource x:Name="dataSource" 
      Source="{StaticResource customerSampleData}">
      <CollectionViewSource.SortDescriptions>
        <compMod:SortDescription PropertyName="Age" Direction="Ascending"/>
      </CollectionViewSource.SortDescriptions>
    </CollectionViewSource>
  </UserControl.Resources>

  <Grid x:Name="LayoutRoot" Background="White">
    <sdk:DataGrid ItemsSource="{Binding Source={StaticResource dataSource}}" />
  </Grid>

</UserControl>

Version Information

Silverlight

Supported in: 5, 4, 3

Silverlight for Windows Phone

Supported in: Windows Phone OS 7.1, Windows Phone OS 7.0

Platforms

For a list of the operating systems and browsers that are supported by Silverlight, see Supported Operating Systems and Browsers.