Click to Rate and Give Feedback
MSDN
MSDN Library
.NET Development
.NET Framework 3.5
Collapse All/Expand All Collapse All
This page is specific to
Microsoft Visual Studio 2008/.NET Framework 3.5

Other versions are also available for the following:
.NET Framework Class Library
ListCollectionView Class

Updated: July 2008

Represents the collection view for collections that implement IList.

Namespace:  System.Windows.Data
Assembly:  PresentationFramework (in PresentationFramework.dll)
Visual Basic (Declaration)
Public Class ListCollectionView _
    Inherits CollectionView _
    Implements IComparer
Visual Basic (Usage)
Dim instance As ListCollectionView
C#
public class ListCollectionView : CollectionView, 
    IComparer
Visual C++
public ref class ListCollectionView : public CollectionView, 
    IComparer
JScript
public class ListCollectionView extends CollectionView implements IComparer
XAML
You cannot directly create an instance of this class in XAML.

When you bind to a data collection, you may want to sort, filter, or group the data. To do that, you use collection views. You can think of a CollectionView as the layer on top of the binding source collection that allows you to navigate and display the source collection based on sort, filter, and group 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.

All collections have a default CollectionView. For all collections implementing IList, the ListCollectionView object is the default view object. The BindingListCollectionView is the collection view class used for collections that implement IBindingList. To get the default view, you use the GetDefaultView method. For an example, see How to: Get the Default View of a Data Collection.

For more information about collection views, see Data Binding Overview.

This example describes how to sort data in a view.

The following example creates a simple ListBox and a Button:

XAML
<Window x:Class="ListBoxSort_snip.Window1"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    Title="ListBoxSort_snip" Height="300" Width="300">
    <DockPanel>
      <ListBox Name="myListBox" DockPanel.Dock="Top">
        <ListBoxItem>my</ListBoxItem>
        <!--Or you can set the content this way:-->
        <!--<ListBoxItem Content="my"/>-->
        <ListBoxItem>1</ListBoxItem>
        <ListBoxItem>Sort</ListBoxItem>
        <ListBoxItem>3</ListBoxItem>
        <ListBoxItem>ListBox</ListBoxItem>
        <ListBoxItem>2</ListBoxItem>
      </ListBox>
      <Button Click="OnClick" Width="30" Height="20" DockPanel.Dock="Top">Sort</Button>
    </DockPanel>
</Window>

The Click event handler of the button contains logic to sort the items in the ListBox in the descending order. You can do this because adding items to a ListBox this way adds them to the ItemCollection of the ListBox, and ItemCollection derives from the CollectionView class. If you are binding your ListBox to a collection using the ItemsSource property, you can use the same technique to sort.

C#
private void OnClick(object sender, RoutedEventArgs e)
{
    myListBox.Items.SortDescriptions.Add(
        new SortDescription("Content", ListSortDirection.Descending));
}

As long as you have a reference to the view object, you can use the same technique to sort the content of other collection views. For an example of how to obtain a view, see How to: Get the Default View of a Data Collection. For another example, see How to: Sort a GridView Column When a Header Is Clicked. For more information about views, see Binding to Collections in Data Binding Overview.

For an example of how to apply sorting logic in Extensible Application Markup Language (XAML), see How to: Sort and Group Data Using a View in XAML.

System..::.Object
  System.Windows.Threading..::.DispatcherObject
    System.Windows.Data..::.CollectionView
      System.Windows.Data..::.ListCollectionView
Any public static (Shared in Visual Basic) members of this type are thread safe. Any instance members are not guaranteed to be thread safe.

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

Date

History

Reason

July 2008

Added new member: CanAddNew property, CanCancelEdit property, CanRemove property, CurrentAddItem property, CurrentEditItem property, Filter property, IsAddingNew property, IsEditingItem property, ItemProperties property, NewItemPlaceholderPosition property, CommitNew method, CommitEdit method, CancelNew method, CancelEdit method, AddNew method, EditItem method, RemoveAt method, Remove method.

SP1 feature change.

Community Content   What is Community Content?
Add new content RSS  Annotations
How might you go about specifying automatic sorting of the collection list view?      Drew Noakes   |   Edit   |   Show History
This documentation explains how a collection list view can be sorted as a one-time operation. Despite some reading around MSDN, I haven't found the means of specifying sorting logic that should be automatically applied as the members of a collection change their values. Is this capability provided, and if not how might it be added?
Tags What's this?: Add a tag
Flag as ContentBug
Sorting not working if providing an empty collection to the construtor      Ma3ztro   |   Edit   |   Show History

I observed that providing an empty list (i.e. an ObservableCollection<T>, in my case) to the ListCollectionView construtor (having a SortDescription criteria attached to it) does not sort new items adding to the observable collection. Although, if the same intial list is provided not empty, then the ListCollectionView sorts new items implicitly (i.e. automatically) no matter if manipulations lead the observable collection empty again.

That is, having one item in the source list when constructing the collection view, deleting the item from the source list, and then adding new items, results in a ListViewCollection sorted as expected.

It looks like that the ListViewCollection does not attach to the CollectionChanged event if the list empty (I did not investigated deeper using Reflector).

Here is a code snippet showing the unexpected behavior:

  

privateclassItem

{

internalstring Title { get; privateset; }

internal Item(string title)

{

Title = title;

}

}


publicvoid TestListCollectionViewBug()

{

var collection1 = newObservableCollection<Item> { newItem("X") };

var collection2 = newObservableCollection<Item>();

var view1 = newListCollectionView(collection1);

var view2 = newListCollectionView(collection2);

view1.SortDescriptions.Add(newSortDescription("Title", ListSortDirection.Ascending));

view2.SortDescriptions.Add(newSortDescription("Title", ListSortDirection.Ascending));

collection1.Add(newItem("C")); collection2.Add(newItem("C"));

collection1.Add(newItem("A")); collection2.Add(newItem("A"));

collection1.Add(newItem("B")); collection2.Add(newItem("B"));

Debug.Assert(((Item)view1.GetItemAt(0)).Title == "A");

Debug.Assert(((Item)view1.GetItemAt(1)).Title == "B");

Debug.Assert(((Item)view1.GetItemAt(2)).Title == "C");

Debug.Assert(((Item)view1.GetItemAt(3)).Title == "X");

Debug.Assert(((Item)view2.GetItemAt(0)).Title == "B");

Debug.Assert(((Item)view2.GetItemAt(1)).Title == "A");

Debug.Assert(((Item)view2.GetItemAt(2)).Title == "C");

}

Tags What's this?: Add a tag
Flag as ContentBug
Processing
© 2009 Microsoft Corporation. All rights reserved. Terms of Use | Trademarks | Privacy Statement | Site Feedback
Page view tracker