31

Has anyone had a go at creating a view that sorts a collection? The ICollectionView interface is present, and it claims to have the same responsibilities as its WPF / SL counterpart (sorting, paging, filtering), however, unless I have missed something, there are no sort methods / properties on the interface.

I would be interested to find out how to take a collection of items then sort them via a view in WinRT.

Note, I know I can do this manually, however, I want to see how a sorted collection interacts with the WinRT theme transitions that appear to add visual effects when sorting is performed.

Steve Rowe
  • 19,411
  • 9
  • 51
  • 82
ColinE
  • 68,894
  • 15
  • 164
  • 232
  • I suspect they have simply not got around to adding the filtering and sorting yet. They do have a method for accessing the groups though. – Phil Wright Oct 31 '11 at 05:47
  • I'm sure Tim would tell you if you asked him. – Filip Skakun Jun 12 '12 at 04:54
  • When I do sorting manually, (i.e bind to an observable collection and add/remove items to the collection) I see some transitions happening in the GridView/ListView. Perhaps this will be similar/same whether you sort it via CollectionViewSource or your own code? – Krishna Jun 13 '12 at 00:23

2 Answers2

4

Unfortunately, there's no support for sorting a collection view in Win8 (nor filtering or grouping). The only way to do this is to manipulate the data source directly, then assign it to Source property.

This has been discussed as an improvement for the post-Win8 timeframe. Wish I had better news :)

Tawnos
  • 1,877
  • 1
  • 17
  • 26
  • 3
    That is not great news. It's a shame that features such as this, that are quite important to business applications and have been present in WPF for years have not made the cut for Win8 v.1 – ColinE Jul 04 '12 at 14:52
2

Linq seems to be the suggested way now that Sort and Filter have gone AWOL.

So you could adopt something like this in your model:

    private MyDataSourceProvider dataSource;
    private ObservableCollection<MyType> sortedDataBackingField;

    public ObservableCollection<MyType> SortedData
    {
        get
        {
            return sortedDataBackingField;
        }
        set
        {
            sortedDataBackingField = value;
            NotifyPropertyChanged("SortedData");
        }
    }


    public void SortByName()
    {
        SortedData = new ObservableCollection<MyType>(dataSource.Entities.OrderBy(
          entity => entity.Name));
    }

    public void SortByAge()
    {
        SortedData = new ObservableCollection<MyType>(dataSource.Entities.OrderBy(
           entity => entity.Age));
    }

Hook SortByName and SortByAge up to your UI in the pattern of your choice, and simply bind to the SortedData property:

<ItemsControl ItemsSource=”{Binding SortedData}”/>

Edit: With reference to transitions, you should find that this approach will trigger the AddDeleteThemeTransition for the items that you've sorted; just add something like this inside the ItemsControl:

<ItemsControl.ItemContainerTransitions>
    <TransitionCollection>
        <AddDeleteThemeTransition></AddDeleteThemeTransition>
    </TransitionCollection>
</ItemsControl.ItemContainerTransitions>
Duncan
  • 29
  • 2
  • One problem with this approach is that if your UI is bound to the ObservableCollection, the whole things gets replaced every time the collection is sorted, i.e. every element in the collection is replaced, rather than being moved. (i.e. sortedDataBackingField.Move(...) ) – Carlos P Aug 09 '12 at 12:39
  • 2
    This is not a solution. For those of us who've been working on responsive UIs, this is an abomination. You can't RESET the collection each time an element changes. – Quark Soup Apr 19 '14 at 00:58