silverlight - PagedCollectionView自定义排序

标签 silverlight silverlight-4.0 mvvm

是否可以在Silverlight4中的PagedCollectionView中自定义排序?
在我看来,我有可能按给定的属性对这些集合进行排序。如果要对集合进行升序或降序排序,我也可以设置。但是我看不到设置自定义排序的可能性-使用某种比较器或类似的东西。

可以通过这种方式实现最简单的排序

PlayerPagedCollection = new PagedCollectionView();
PlayerPagedCollection.SortDescriptions.Clear();
PlayerPagedCollection.SortDescriptions.Add(new System.ComponentModel.SortDescription("Name",ListSortDirection.Ascending)); 

是否有可能以某种方式设置自定义排序?我需要使其在Silverlight4上运行

最佳答案

额外的复杂性并不理想,但这对我有用。

public class YourViewModel
{
    private YourDomainContext context;
    private IOrderedEnumerable<Person> people;
    private PagedCollectionView view;
    private PersonComparer personComparer;

    public YourViewModel()
    {
        context = new YourDomainContext();
        personComparer = new PersonComparer()
        {
            Direction = ListSortDirection.Ascending
        };
        people = context.People.OrderBy(p => p, personComparer);
        view = new PagedCollectionView(people);
    }

    public void Sort()
    {
        using (view.DeferRefresh())
        {
            personComparer.Direction = ListSortDirection.Ascending;

            //this triggers the IOrderedEnumerable to resort
            FlightTurnaroundProcessesView.SortDescriptions.Clear();
        }
    }
}

public class PersonComparer : IComparer<Person>
{
    public ListSortDirection Direction { get; set; }

    public int Compare(Person x, Person y)
    {
        //add any custom sorting here
        return x.CompareTo(y) * GetDirection();
    }

    private int GetDirection()
    {
        return Direction == ListSortDirection.Ascending ? 1 : -1;
    }
}

关于silverlight - PagedCollectionView自定义排序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8193611/

相关文章:

Silverlight (3.0) : How to add cell padding to a Grid?

c# - 指南针 UI 和指针

c# - 使用 Silverlight 在运行时在 tabcontrol 中添加 TabItem

wpf - Model-View-Presenter 和 Modal 对话框.... 如何?

c# - 通过 ViewModel 设置 ImageSource 不起作用

silverlight - 微软 Azure 和 Silverlight

.net - Silverlight 本地存储

c# - 从 SQL Server 2008 R2 解密数据

c# - 在哪里可以找到 .Net framework 4 或 silverlight 类的层次结构图?

wpf - 如何正确实现文本框验证