Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Creating a Sorted Collection View in WinRT

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.

like image 360
ColinE Avatar asked Oct 04 '11 14:10

ColinE


2 Answers

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 :)

like image 91
Tawnos Avatar answered Nov 13 '22 23:11

Tawnos


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>
like image 2
Duncan Avatar answered Nov 13 '22 22:11

Duncan