您能帮我使用映射到 observablecollection 的 PagedCollectionview 自定义 Silverlight 中的排序吗?下面是排序部分的代码,但它不会刷新网格,因为第一列的排序未清除
例如。如果我使用“描述”对它进行排序,它可以在两个方向(asc & desc)上工作。但是在使用“描述”对集合进行排序后,如果我单击“类型”标题,它应该使用“类型”清除较早的排序和排序" 仅列。
private void SortCollectionChanged(object sender, NotifyCollectionChangedEventArgs e)
{
if (e.Action == NotifyCollectionChangedAction.Remove || e.Action == NotifyCollectionChangedAction.Reset)
return;
if (e.Action == NotifyCollectionChangedAction.Replace || e.Action == NotifyCollectionChangedAction.Add)
{
MyPVC.SortDescriptions.Clear();
if (e.NewItems.Count > 0)
{
MyPVC.SortDescriptions.Clear();
SortDescription sd = (SortDescription) e.NewItems[0];
if (sd.PropertyName == "description")
{
e.NewItems.Clear();
using (MyPVC.DeferRefresh())
{
ObservableCollection<MyClass> source = ((ObservableCollection<MyClass>)MyPVC.SourceCollection);
if (source == null)
return;
bool asc = (sd.Direction == ListSortDirection.Ascending);
var source1 = new List<MyClass>(source);
source1.Sort((a, b) =>
{
int left = 0;
int right = 0;
var ret = 0;
if (int.TryParse(a.description, out left) && int.TryParse(b.description, out right))
{
ret = (left < right) ? -1 : (left == right) ? 0 : 1;
if (!asc)
ret = -ret;
}
return ret;
});
var newsource = new ObservableCollection<MyClass>(source1);
MyPVC = new PagedCollectionView(newsource);
((INotifyCollectionChanged)MyPVC.SortDescriptions).CollectionChanged += SortCollectionChanged;
MyPVC.SortDescriptions.Clear();
}
MyClassDataGrid.ItemsSource = MyPVC;
MyPVC.Refresh();
}
if (sd.PropertyName == "Type")
{
MyPVC.SortDescriptions.Clear();
//MyPVC = new PagedCollectionView(MyPVC);
e.NewItems.Clear();
using (MyPVC.DeferRefresh())
{
ObservableCollection<MyClass> source = ((ObservableCollection<MyClass>)MyPVC.SourceCollection);
if (source == null)
return;
bool asc = (sd.Direction == ListSortDirection.Ascending);
var source1 = new List<MyClass>(source);
source1.Sort((a, b) =>
{
int left = 0;
int right = 0;
var ret = 0;
if (int.TryParse(a.Type, out left) && int.TryParse(b.Type, out right))
{
ret = (left < right) ? -1 : (left == right) ? 0 : 1;
if (!asc)
ret = -ret;
}
return ret;
});
var newsource = new ObservableCollection<MyClass>(source1);
MyPVC = new PagedCollectionView(newsource);
((INotifyCollectionChanged)MyPVC.SortDescriptions).CollectionChanged += SortCollectionChanged;
MyPVC.SortDescriptions.Clear();
}
}
}
}
}
private void MyClasstableView_ColumnHeaderClick(object sender, ColumnHeaderClickEventArgs e)
{
((INotifyCollectionChanged) MyPVC.SortDescriptions).CollectionChanged += SortCollectionChanged;
}