Compartilhar via


CollectionViewSource Class

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

The XAML proxy of a collection view class.

Inheritance Hierarchy

System.Object
  System.Windows.DependencyObject
    System.Windows.Data.CollectionViewSource

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

Syntax

'Declaration
Public Class CollectionViewSource _
    Inherits DependencyObject _
    Implements ISupportInitialize
public class CollectionViewSource : DependencyObject, 
    ISupportInitialize
<CollectionViewSource .../>

The CollectionViewSource type exposes the following members.

Constructors

  Name Description
Public methodSupported by Silverlight for Windows Phone CollectionViewSource Initializes a new instance of the CollectionViewSource class.

Top

Properties

  Name Description
Public propertySupported by Silverlight for Windows Phone Culture Gets or sets the cultural information for any operations of the view that might differ by culture, such as sorting.
Public propertySupported by Silverlight for Windows Phone Dispatcher Gets the Dispatcher this object is associated with. (Inherited from DependencyObject.)
Public propertySupported by Silverlight for Windows Phone GroupDescriptions Gets a collection of GroupDescription objects that describe how items in the collection are grouped in the view.
Public propertySupported by Silverlight for Windows Phone SortDescriptions Gets a collection of SortDescription objects that describe how the items in the collection are sorted in the view.
Public propertySupported by Silverlight for Windows Phone Source Gets or sets the collection object from which to create this view.
Public propertySupported by Silverlight for Windows Phone View Gets the view object that is currently associated with this instance of CollectionViewSource.

Top

Methods

  Name Description
Public methodSupported by Silverlight for Windows Phone CheckAccess Determines whether the calling thread has access to this object. (Inherited from DependencyObject.)
Public methodSupported by Silverlight for Windows Phone ClearValue Clears the local value of a dependency property. (Inherited from DependencyObject.)
Public methodSupported by Silverlight for Windows Phone DeferRefresh Enters a defer cycle that you can use to merge changes to the view and delay automatic refresh.
Public methodSupported by Silverlight for Windows Phone Equals(Object) Determines whether the specified Object is equal to the current Object. (Inherited from Object.)
Protected methodSupported by Silverlight for Windows Phone Finalize Allows an object to try to free resources and perform other cleanup operations before the Object is reclaimed by garbage collection. (Inherited from Object.)
Public methodSupported by Silverlight for Windows Phone GetAnimationBaseValue Returns any base value established for a Silverlight dependency property, which would apply in cases where an animation is not active. (Inherited from DependencyObject.)
Public methodSupported by Silverlight for Windows Phone GetHashCode Serves as a hash function for a particular type. (Inherited from Object.)
Public methodSupported by Silverlight for Windows Phone GetType Gets the Type of the current instance. (Inherited from Object.)
Public methodSupported by Silverlight for Windows Phone GetValue Returns the current effective value of a dependency property from a DependencyObject. (Inherited from DependencyObject.)
Protected methodSupported by Silverlight for Windows Phone MemberwiseClone Creates a shallow copy of the current Object. (Inherited from Object.)
Protected methodSupported by Silverlight for Windows Phone OnCollectionViewTypeChanged Invoked when the collection view type changes.
Protected methodSupported by Silverlight for Windows Phone OnSourceChanged Invoked when the Source property changes.
Public methodSupported by Silverlight for Windows Phone ReadLocalValue Returns the local value of a dependency property, if a local value is set. (Inherited from DependencyObject.)
Public methodSupported by Silverlight for Windows Phone SetValue Sets the local value of a dependency property on a DependencyObject. (Inherited from DependencyObject.)
Public methodSupported by Silverlight for Windows Phone ToString Returns a string that represents the current object. (Inherited from Object.)

Top

Events

  Name Description
Public eventSupported by Silverlight for Windows Phone Filter Provides filtering logic.

Top

Fields

  Name Description
Public fieldStatic memberSupported by Silverlight for Windows Phone SourceProperty Identifies the Source dependency property.
Public fieldStatic memberSupported by Silverlight for Windows Phone ViewProperty Identifies the View dependency property.

Top

Explicit Interface Implementations

  Name Description
Explicit interface implemetationPrivate methodSupported by Silverlight for Windows Phone ISupportInitialize.BeginInit Signals the object that initialization is starting.
Explicit interface implemetationPrivate methodSupported by Silverlight for Windows Phone ISupportInitialize.EndInit Signals the object that initialization is complete.

Top

Remarks

CollectionViewSource is a proxy for a class that implements ICollectionView. CollectionViewSource enables XAML code to set the commonly used ICollectionView properties, passing these settings to the underlying view. CollectionViewSource has a View property that holds the actual view and a Source property that holds the source collection. The view is generated automatically unless the source collection implements ICollectionViewFactory, in which case the view is retrieved through the CreateView method.

You can think of a collection view as the layer on top of the binding source collection that allows you to navigate and display the collection based on sort, filter, and grouping queries, all without having to manipulate the underlying source collection itself. If the source collection implements the INotifyCollectionChanged interface, the changes raised by the CollectionChanged event are propagated to the views.

Because views do not change the underlying source collections, each source collection can have multiple views associated with it. For example, you may have a collection of Task objects. With the use of views, you can display that same data in different ways. For example, on the left side of your page you may want to show tasks sorted by priority, and on the right side, grouped by area.

Version Notes

Silverlight 3 does not support the ICollectionViewFactory interface.

Examples

The following code example demonstrates the use of this class 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.

Thread Safety

Any public static (Shared in Visual Basic) members of this type are thread safe. Any instance members are not guaranteed to be thread safe.