Creating a Sorted Collection View in WinRT

╄→尐↘猪︶ㄣ 提交于 2019-12-04 16:12:37

问题


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.


回答1:


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




回答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>


来源:https://stackoverflow.com/questions/7649532/creating-a-sorted-collection-view-in-winrt

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!