CollectionViewSource CollectionViewSource CollectionViewSource Class

Provides a data source that adds grouping and current-item support to collection classes.

Syntax

Declaration

public sealed class CollectionViewSourcepublic sealed class CollectionViewSourcePublic NotInheritable Class CollectionViewSource
<CollectionViewSource .../>

Inheritance Hierarchy

Remarks

Use CollectionViewSource when you want to bind list controls to collections, but you want to display those collections in groups and maintain a current item independent from the list control. This is particularly useful when you want to bind multiple controls to the same collection and you want the current item in one control to change the current item in the other bound controls. You typically define a CollectionViewSource as a XAML resource and bind to it using the {StaticResource} markup extension. You can then set its Source property in code-behind to a supported collection type.

Any controls that you bind to the same CollectionViewSource will always have the same current item. You can access the current item programmatically through the CurrentItem property of the View property value.

If the items in the collection are collections themselves, or are objects that contain collections, you can display the collections as groups within the larger collection. To do this, set the IsSourceGrouped property to true. If the items contain collections but are not collections themselves, you must also set the ItemsPath property to the name of the collection property.

Note

Setting the Source property to another CollectionViewSource instance is not supported.

Examples

The following code example demonstrates how to bind a ListBox control to the results of a grouping LINQ query. In this example, a collection of teams is grouped by city and displayed with the city name as the group headers. For the complete code listing, see the XAML data binding sample. For additional example code on grouping, see the XAML GridView grouping and SemanticZoom sample.

<Grid>

  <Grid.Resources>
    <CollectionViewSource x:Name="groupInfoCVS" IsSourceGrouped="true"/>
  </Grid.Resources>

  <ListBox x:Name="lbGroupInfoCVS" 
    ItemsSource="{Binding Source={StaticResource groupInfoCVS}}">

    <ListBox.GroupStyle>
      <GroupStyle>
        <GroupStyle.HeaderTemplate>
          <DataTemplate>

            <TextBlock Text="{Binding Key}"/>

          </DataTemplate>
        </GroupStyle.HeaderTemplate>
      </GroupStyle>
    </ListBox.GroupStyle>

    <ListBox.ItemTemplate>
      <DataTemplate>

        <Border Background="{Binding Color}" 
          Width="200" CornerRadius="10" HorizontalAlignment="Left">

          <TextBlock Text="{Binding Name}" 
            Style="{StaticResource DescriptionTextStyle}" 
            HorizontalAlignment="Center" FontWeight="Bold"/>

        </Border>
      </DataTemplate>
    </ListBox.ItemTemplate>

  </ListBox>

</Grid>
Teams teams = new Teams();
var result = 
    from t in teams 
    group t by t.City into g 
    orderby g.Key 
    select g;
groupInfoCVS.Source = result;

Constructors summary

Initializes a new instance of the CollectionViewSource class.

Properties summary

Gets or sets a value that indicates whether source data is grouped.

Identifies the IsSourceGrouped dependency property.

Gets or sets the property path to follow from the top level item to find groups within the CollectionViewSource.

Identifies the ItemsPath dependency property.

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

Identifies the Source dependency property.

Gets the view object that is currently associated with this instance of CollectionViewSource.

Identifies the View dependency property.

Constructors

  • CollectionViewSource()
    CollectionViewSource()
    CollectionViewSource()
    CollectionViewSource()

    Initializes a new instance of the CollectionViewSource class.

    public CollectionViewSource()public CollectionViewSource()Public Function CollectionViewSource() As

Properties

  • IsSourceGrouped
    IsSourceGrouped
    IsSourceGrouped
    IsSourceGrouped

    Gets or sets a value that indicates whether source data is grouped.

    public bool IsSourceGrouped { get; set; }public bool IsSourceGrouped { get; set; }Public ReadWrite Property IsSourceGrouped As bool
    <CollectionViewSource IsSourceGrouped="bool" .../>
    

    Property Value

    • bool
      bool
      bool

      true if data is grouped. false if data is not grouped.

    Remarks

    Examples

    The following code example demonstrates how to bind a ListBox control to the results of a grouping LINQ query. In this example, a collection of teams is grouped by city and displayed with the city name as the group headers. For the complete code listing, see the XAML data binding sample. For additional example code on grouping, see the Grouped GridView sample.

    <Grid>
    
      <Grid.Resources>
        <CollectionViewSource x:Name="groupInfoCVS" IsSourceGrouped="true"/>
      </Grid.Resources>
    
      <ListBox x:Name="lbGroupInfoCVS" 
        ItemsSource="{Binding Source={StaticResource groupInfoCVS}}">
    
        <ListBox.GroupStyle>
          <GroupStyle>
            <GroupStyle.HeaderTemplate>
              <DataTemplate>
    
                <TextBlock Text="{Binding Key}"/>
    
              </DataTemplate>
            </GroupStyle.HeaderTemplate>
          </GroupStyle>
        </ListBox.GroupStyle>
    
        <ListBox.ItemTemplate>
          <DataTemplate>
    
            <Border Background="{Binding Color}" 
              Width="200" CornerRadius="10" HorizontalAlignment="Left">
    
              <TextBlock Text="{Binding Name}" 
                Style="{StaticResource DescriptionTextStyle}" 
                HorizontalAlignment="Center" FontWeight="Bold"/>
    
            </Border>
          </DataTemplate>
        </ListBox.ItemTemplate>
    
      </ListBox>
    
    </Grid>
    
    Teams teams = new Teams();
    var result = 
        from t in teams 
        group t by t.City into g 
        orderby g.Key 
        select g;
    groupInfoCVS.Source = result;
    
  • IsSourceGroupedProperty
    IsSourceGroupedProperty
    IsSourceGroupedProperty
    IsSourceGroupedProperty

    Identifies the IsSourceGrouped dependency property.

    public static DependencyProperty IsSourceGroupedProperty { get; }public static DependencyProperty IsSourceGroupedProperty { get; }Public Static ReadOnly Property IsSourceGroupedProperty As DependencyProperty

    Property Value

  • ItemsPath
    ItemsPath
    ItemsPath
    ItemsPath

    Gets or sets the property path to follow from the top level item to find groups within the CollectionViewSource.

    public PropertyPath ItemsPath { get; set; }public PropertyPath ItemsPath { get; set; }Public ReadWrite Property ItemsPath As PropertyPath
    <CollectionViewSource ItemsPath="propertyPathString" .../>
    

    Property Value

  • ItemsPathProperty
    ItemsPathProperty
    ItemsPathProperty
    ItemsPathProperty

    Identifies the ItemsPath dependency property.

    public static DependencyProperty ItemsPathProperty { get; }public static DependencyProperty ItemsPathProperty { get; }Public Static ReadOnly Property ItemsPathProperty As DependencyProperty

    Property Value

  • Source
    Source
    Source
    Source

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

    public object Source { get; set; }public object Source { get; set; }Public ReadWrite Property Source As object
    <CollectionViewSource Source="bindingReference" .../>
    

    Property Value

    • object
      object
      object

      The collection to create the view from.

    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.

  • SourceProperty
    SourceProperty
    SourceProperty
    SourceProperty

    Identifies the Source dependency property.

    public static DependencyProperty SourceProperty { get; }public static DependencyProperty SourceProperty { get; }Public Static ReadOnly Property SourceProperty As DependencyProperty

    Property Value

  • View
    View
    View
    View

    Gets the view object that is currently associated with this instance of CollectionViewSource.

    public ICollectionView View { get; }public ICollectionView View { get; }Public ReadOnly Property View As ICollectionView

    Property Value

    Remarks

    This property is initialized when you set the Source property. At that time, a view is generated automatically unless the source collection implements ICollectionViewFactory. Otherwise, the view is retrieved through the CreateView() method.

  • ViewProperty
    ViewProperty
    ViewProperty
    ViewProperty

    Identifies the View dependency property.

    public static DependencyProperty ViewProperty { get; }public static DependencyProperty ViewProperty { get; }Public Static ReadOnly Property ViewProperty As DependencyProperty

    Property Value

Device family

Windows 10 (introduced v10.0.10240.0)

API contract

Windows.Foundation.UniversalApiContract (introduced v1)

Attributes

Windows.Foundation.Metadata.MarshalingBehaviorAttribute
Windows.Foundation.Metadata.ContractVersionAttribute
Windows.Foundation.Metadata.ThreadingAttribute
Windows.Foundation.Metadata.ActivatableAttribute
Windows.Foundation.Metadata.WebHostHiddenAttribute
Windows.Foundation.Metadata.StaticAttribute

Details

Assembly

Windows.UI.Xaml.Data.dll