你能帮我定制Silverlight中的排序吗?使用PagedCollectionview映射到observablecollection.Below是一个适用于排序部分的代码,但由于没有清除第一列的排序,它不会刷新网格
EG。如果我使用“描述”对它进行排序,它可以在方向(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;
}
答案 0 :(得分:1)
看起来你在这里做了太多的工作来实现一个简单的排序。 PagedCollectionView旨在过滤,排序等。它将为您完成工作。你有正确的基础,但你不必进行源操作以使排序工作。
答案 1 :(得分:0)
Thanx为您的回复Dane,但该集合包含字符串格式的所有数据,我需要根据日期对某些数据进行排序,因此我需要在其中使用转换。因此我需要这些操作。我有我对代码进行了一些更改,因为我需要更新网格,我已经使用了begindataupdate() - EndDataUpdate()方法,但它给了我一个错误。我相信它来自UI。
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)LeasePVC.SortDescriptions).CollectionChanged += SortCollectionChanged;
//LeasePVC.Refresh();
LeasePVC.SortDescriptions.Clear();
this.MyClassDataGrid.BeginDataUpdate();
this.MyClassDataGrid.ItemsSource = MyPVC;
this.MyClassDataGrid.EndDataUpdate();
}
}
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)LeasePVC.SortDescriptions).CollectionChanged += SortCollectionChanged;
//LeasePVC.Refresh();
LeasePVC.SortDescriptions.Clear();
this.MyClassDataGrid.BeginDataUpdate();
this.MyClassDataGrid.ItemsSource = MyPVC;
this.MyClassDataGrid.EndDataUpdate();
}
}
}
}
}
private void MyClasstableView_ColumnHeaderClick(object sender, ColumnHeaderClickEventArgs e)
{
((INotifyCollectionChanged) MyPVC.SortDescriptions).CollectionChanged += SortCollectionChanged;
}