.net PagedCollectionview中的自定义排序&;银光

.net PagedCollectionview中的自定义排序&;银光,.net,silverlight,sorting,devexpress,pagedcollectionview,.net,Silverlight,Sorting,Devexpress,Pagedcollectionview,您能否帮助我使用映射到observablecollection的PagedCollectionview在silverlight中自定义排序。下面是排序部分工作正常的代码,但它不会刷新网格,因为第一列的排序未清除 例如,如果我使用“描述”对其进行排序,则它在两个方向(asc和desc)上都有效。但是在使用“描述”对集合进行排序后,如果我单击“类型”标题,则应仅使用“类型”列清除前面的排序和排序 private void SortCollectionChanged(object sender,

您能否帮助我使用映射到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;
    }
private void SortCollectionChanged(对象发送方,NotifyCollectionChangedEventArgs e)
{
if(e.Action==NotifyCollectionChangedAction.Remove | | e.Action==NotifyCollectionChangedAction.Reset)
返回;
如果(e.Action==NotifyCollectionChangedAction.Replace | | e.Action==NotifyCollectionChangedAction.Add)
{
MyPVC.SortDescriptions.Clear();
如果(e.NewItems.Count>0)
{
MyPVC.SortDescriptions.Clear();
SortDescription sd=(SortDescription)e.NewItems[0];
如果(sd.PropertyName==“说明”)
{
e、 NewItems.Clear();
使用(MyPVC.DeferRefresh())
{
ObservableCollection源=((ObservableCollection)MyPVC.SourceCollection);
if(source==null)
返回;
bool asc=(sd.Direction==ListSortDirection.Ascending);
var source1=新列表(源);
source1.排序((a,b)=>
{
int左=0;
int right=0;
var-ret=0;
if(int.TryParse(a.description,左外)和&int.TryParse(b.description,右外))
{
ret=(左<右)?-1:(左==右)?0:1;
如果(!asc)
ret=-ret;
}
返回ret;
});
var newsource=新的ObservableCollection(source1);
MyPVC=新页面集合视图(newsource);
((INotifyCollectionChanged)MyPVC.SortDescriptions)。CollectionChanged+=SortCollectionChanged;
MyPVC.SortDescriptions.Clear();
}
MyClassDataGrid.ItemsSource=MyPVC;
MyPVC.Refresh();
}
if(sd.PropertyName==“Type”)
{
MyPVC.SortDescriptions.Clear();
//MyPVC=新页面集合视图(MyPVC);
e、 NewItems.Clear();
使用(MyPVC.DeferRefresh())
{
ObservableCollection源=((ObservableCollection)MyPVC.SourceCollection);
if(source==null)
返回;
bool asc=(sd.Direction==ListSortDirection.Ascending);
var source1=新列表(源);
source1.排序((a,b)=>
{
int左=0;
int right=0;
var-ret=0;
if(int.TryParse(a.Type,左外)和&int.TryParse(b.Type,右外))
{
ret=(左<右)?-1:(左==右)?0:1;
如果(!asc)
ret=-ret;
}
返回ret;
});
var newsource=新的ObservableCollection(source1);
MyPVC=新页面集合视图(newsource);
((INotifyCollectionChanged)MyPVC.SortDescriptions)。CollectionChanged+=SortCollectionChanged;
MyPVC.SortDescriptions.Clear();
}
}
}
}
}
私有void MyClasstableView\u ColumnHeaderClick(对象发送方,ColumnHeaderClickEventArgs e)
{
((INotifyCollectionChanged)MyPVC.SortDescriptions)。CollectionChanged+=SortCollectionChanged;
}

看起来您在这里做的工作太多,无法实现简单的排序。PagedCollectionView用于过滤、排序等。。它将为你做这项工作。您已经掌握了基本知识,但不必为了使排序正常工作而进行源代码操作。

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;
    }
private void SortCollectionChanged(对象发送方,NotifyCollectionChangedEventArgs e)
{
if(e.Action==NotifyCollectionChangedAction.Remove | | e.Action==NotifyCollectionChangedAction.Reset)
返回;
如果(e.Action==NotifyCollectionChangedAction.Replace | | e.Action==NotifyCollectionChangedAction.Add)
{
MyPVC.SortDescriptions.Clear();
如果(e.NewItems.Count>0)
{
MyPVC.SortDescriptions.Clear();
SortDescription sd=(SortDescription)e.NewItems[0];
如果(sd.PropertyName==“说明”)
{
e、 NewItems.Clear();
使用(MyPVC.DeferRefresh())
{
可观测收集源=