2012-04-19 17 views
1

検索クエリに基づいてDataGridの行をハイライト表示する簡単な検索機能を実装しました。これの要点を以下に示す:ソートされたDataGridの検索

public bool scrollToSearch(string query) { 
    dataGrid.SelectedItems.Clear(); 

    for (; searchIndex < registrants.Count; searchIndex++) { 
     foreach (string field in registrants[searchIndex]) { 
      if (field.ToLower().Contains(query)) { 
       dataGrid.SelectedItem = registrants[searchIndex]; 
       dataGrid.ScrollIntoView(registrants[searchIndex]); 
       searchIndex++; 
       return true; 
      } 
     } 
    }  
}  

それが一致するリストを検索し、その行ビューにスクロールして(選択)を強調しています。問題は、DataGridをソートすると、最初の結果ではなく一見ランダムな行が強調表示されることです。ソートされていない元のリストを検索しているからです。ソートされたリストで検索する方法がありますか?

+1

[this](http://stackoverflow.com/questions/8896422/persist-sorting-from-datagrid-to-itemssource-collection)を参照してください。 –

答えて

0

まず

var view = CollectionViewSource.GetDefaultView(registrants); 

、あなたのコード内でregistrantsの代わりにviewを使用....これを行います。

1

私は、ListCollectionViewが適切な方法であるというAnuragのコメントに同意します。 ListCollectionViewのドキュメントについては、See hereを参照してください。 ListCollectionViewはソート、フィルタリング、およびグループ化を処理するソースとディスプレイの間に別のレイヤーを作成します。 DataGridをListCollectionViewにバインドし、SetCurrent()を使用して、選択した項目を指定します。

関連する問題