Export (0) Print
Expand All

CollectionViewSource.Source Property

Gets or sets the collection object from which to create this view. This is a dependency property.

Namespace:  System.Windows.Data
Assembly:  PresentationFramework (in PresentationFramework.dll)
XMLNS for XAML: http://schemas.microsoft.com/winfx/2006/xaml/presentation, http://schemas.microsoft.com/netfx/2007/xaml/presentation

public Object Source { get; set; }
<object>
  <object.Source>
    <Object .../>
  </object.Source>
</object>
<object Source="myCollection" .../>

XAML Values

myCollection

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

Property Value

Type: System.Object
The default value is null.

Identifier Field

SourceProperty

Metadata properties set to true

None

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.

In the following example, the static resource named places is defined as a collection of Place objects, in which each Place object is consisted of a city name and the state. The prefix src is mapped to the namespace where the data source Places is defined. The prefix scm and dat are mapped to the System.ComponentModel and System.Windows.Data namespaces respectively.

The following example creates a view of the data collection that is sorted by the city name and grouped by the state.

<Window.Resources>

  <src:Places x:Key="places"/>

  <CollectionViewSource Source="{StaticResource places}" x:Key="cvs">
    <CollectionViewSource.SortDescriptions>
      <scm:SortDescription PropertyName="CityName"/>
    </CollectionViewSource.SortDescriptions>
    <CollectionViewSource.GroupDescriptions>
      <dat:PropertyGroupDescription PropertyName="State"/>
    </CollectionViewSource.GroupDescriptions>
  </CollectionViewSource>

The view can then be a binding source, as in the following example:

<ListBox ItemsSource="{Binding Source={StaticResource cvs}}"
         DisplayMemberPath="CityName" Name="lb">
  <ListBox.GroupStyle>
    <x:Static Member="GroupStyle.Default"/>
  </ListBox.GroupStyle>
</ListBox>

For bindings to XML data defined in an XmlDataProvider resource, precede the XML name with an @ symbol.

<XmlDataProvider x:Key="myTasks" XPath="Tasks/Task">
    <x:XData>
        <Tasks xmlns="">
            <Task Name="Groceries" Priority="2" Type="Home">
<CollectionViewSource x:Key="mySortedTasks"
                      Source="{StaticResource myTasks}">
    <CollectionViewSource.SortDescriptions>
        <scm:SortDescription PropertyName="@Priority" />
    </CollectionViewSource.SortDescriptions>
    <CollectionViewSource.GroupDescriptions>
        <dat:PropertyGroupDescription PropertyName="@Priority" />
    </CollectionViewSource.GroupDescriptions>
</CollectionViewSource>

To see the entire example, please see Sorting and Grouping Data in XAML Sample.

Windows 7, Windows Vista, Windows XP SP2, Windows Server 2008 R2, Windows Server 2008, Windows Server 2003

The .NET Framework and .NET Compact Framework do not support all versions of every platform. For a list of the supported versions, see .NET Framework System Requirements.

.NET Framework

Supported in: 3.5, 3.0

Community Additions

ADD
Show:
© 2014 Microsoft