【问题标题】:PagedCollectionView custom sortingPagedCollectionView 自定义排序
【发布时间】:2012-01-01 21:03:44
【问题描述】:

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

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

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

是否可以以某种方式设置自定义排序?我需要让它在 Silverlight4 上工作

【问题讨论】:

    标签: silverlight silverlight-4.0 mvvm


    【解决方案1】:

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

    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;
        }
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2013-01-15
      • 2014-09-10
      • 2014-07-18
      • 2011-12-16
      • 2011-01-11
      • 2011-03-08
      • 2014-04-29
      相关资源
      最近更新 更多