2013-10-25 43 views
0

您可以幫我使用PagedCollectionview將自定義排序映射到observablecollection.Below是排序部分工作正常的代碼,但它不會刷新網格作爲從第一列未清除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; 
    } 

回答

0

Thanx爲您的答覆丹麥人,但收集的所有數據的字符串格式,我需要根據日期排序一些數據,所以我需要使用它的轉換。因此,我需要這些操作。我對代碼做了一些更改,因爲我需要更新網格,我已經使用了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; 
    } 
+0

你得到的錯誤是什麼?你能否展示數據的類?這將非常有幫助。 – Danexxtone

+0

thanx丹麥爲你的時間我有一個解決這個問題的方法。我在其他領域的基礎上排序收集 – xoanon

1

它看起來像你在這裏做太多的工作來實現一個簡單的排序。 PagedCollectionView旨在過濾,排序等。它將爲您完成這項工作。您的基本知識是正確的,但您無需執行源操作即可完成排序工作。

+0

thanx您的回覆丹麥人,但集合了以字符串格式的所有數據,我需要的,所以我需要使用轉換在it.hence我需要這些操作迄今爲止的基礎上的一些數據以及排序。我已經對代碼做了一些更改,因爲我需要更新網格,我已經使用了begindataupdate() - EndDataUpdate()方法,但它給了我一個錯誤。我相信它來自UI。 – xoanon