是否可以在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/