Silverlightで、動的に変化する接続から並べ替えられたDataGridを作成する方法

StackOverflow https://stackoverflow.com/questions/234424

  •  04-07-2019
  •  | 
  •  

質問

要素がDataGridの行として表示されるデータセットがあります。行のソート順は、外部イベントに応じて変わります。

最初に考えたのは、行をObservableCollectionとして保存し、更新後にコレクションを再利用することでした。しかし、2つの問題に遭遇しました。 1)ObservableCollectionにはSort()メソッドがありません 2)要素を自分で並べ替えようとすると、要素を新しい位置に割り当てようとすると例外が発生します。たとえば、

class MyCollection : ObservableCollection<T>
{
   void swap( int i, int j )
   {
      T tmp = this[i];
      this[i] = this[j]; // THROWS A NOT SUPPORTED EXCEPTION
      this[j] = tmp;
   }
}

質問は...行の順序を動的に更新する必要があるDataGridにデータを入力する方法ですか?

ようやく1つの答えが機能するようになりました。以下で説明します。

役に立ちましたか?

解決

(ObservableCollectionを使用する代わりに)INotifyCollectionChangedを明示的に実装することで、これが機能するようになりました。さらに、更新アクションを使用すると、同じ「サポート対象外」になることがわかりました。エラーが、追加と削除のアクションを使用できること。したがって、私のスワップ関数は次のようになります:

class MyCollection<T> : List<T>, INotifyCollectionChanged
{
   public event NotifyCollectionChangedEventHandler CollectionChanged;

   private void swap( int i, int j )
   {
      T a = this[i];
      T b = this[j];

      // swap my own internal data storage
      this[i] = b;
      this[j] = a;

      // and also let my CollectionChanged listener know that I have done so.
      if( CollectionChanged != null )
      {
         NotifyCollectionChangedEventArgs arg;

         arg = new NotifyCollectionChangedEventArgs(
             NotifyCollectionChangedAction.Remove, a, i );
         CollectionChanged( this, arg );

         arg = new NotifyCollectionChangedEventArgs(
             NotifyCollectionChangedAction.Add, b, i );
         CollectionChanged( this, arg );

         arg = new NotifyCollectionChangedEventArgs(
             NotifyCollectionChangedAction.Remove, b, j );
         CollectionChanged( this, arg );

         arg = new NotifyCollectionChangedEventArgs(
             NotifyCollectionChangedAction.Add, a, j );
         CollectionChanged( this, arg );

      }

   }

}

動的な変更はかなりローカルであるため、幸いなことに、変更に応じてより低速の手書きソートを使用することは問題ありません。つまり、更新が到着すると、次のような(同じコレクション内の)別のメンバー関数を呼び出します。

public void ProcessUpdates( List<T> updateList )
{
    // use the contents of updateList to modify my internal store
    // ...


    // and now resort myself
    sort();
}

private void sort()
{
    // implement your favorite stable sorting algorithm here, calling 
    // swap() whenever you swap two elements.

    // (this is an intentionally facetious sorting algorithm, because I
    // don't want to get into the long and irrelevant details of my own 
    // data storage.)
    while( i_am_not_sorted() )
    {
       int i = random_index();
       int j = random_index();
       if( out_of_order(i,j) )
       {
          // modify my internal data structure and 
          // also let my CollectionChanged listener know that I have done so
          swap( i, j );
       }
    }
}

「追加」を実行することも必要であることを忘れないでください。コレクションに要素を追加する際の通知!最初のリストを並べ替え、並べ替えられた順序で追加します。これにより、最初にデータを入力するときに、より効率的なライブラリの並べ替えを使用できます。

ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top