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.

4

2 回答 2

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

于 2012-06-24T05:50:27.627 回答
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>
于 2012-07-03T16:27:38.060 回答