是否可以在Silverlight4中的PagedCollectionView中自定义排序? 在我看来,我有可能通过给定的属性对这个集合进行排序。如果我想对集合进行升序或降序排序,我也可以设置。但是我看不到设置自定义排序的可能性 - 使用某种比较器或类似的东西。
最简单的排序可以通过这种方式实现
PlayerPagedCollection = new PagedCollectionView();
PlayerPagedCollection.SortDescriptions.Clear();
PlayerPagedCollection.SortDescriptions.Add(new System.ComponentModel.SortDescription("Name",ListSortDirection.Ascending));
是否有可能以某种方式设置自定义排序?我需要让它在Silverlight4上运行
答案 0 :(得分:0)
额外的复杂性并不理想,但这对我有用。
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;
}
}