Silverlight에서 동적으로 변화하는 연결에서 정렬 된 Datagrid를 채우는 방법

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

  •  04-07-2019
  •  | 
  •  

문제

데이터 그라이드에서 요소가 행으로 표시되는 데이터 세트가 있습니다. 외부 이벤트에 대한 응답으로 행의 정렬 순서가 변경됩니다.

저의 초기 생각은 행을 관측형 수집으로 저장하고 업데이트 후 컬렉션을 리조트하는 것이 었습니다. 그러나 나는 두 가지 문제에 부딪쳤다 : 1) ObservableCollection에는 sort () 메소드가 없다. 기능과 같은 스왑

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;
   }
}

문제는 ... 행 순서가 동적으로 업데이트 해야하는 데이터 그라이드를 채우는 방법입니다.

마침내 하나의 답변을 얻었습니다. 아래에서 설명하겠습니다.

도움이 되었습니까?

해결책

나는 관측형 수집을 사용하는 대신 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