0

「検索文字列」に従ってアイテムをフィルタリングする機能を持つ WPF カスタム ComboBox を作成しました。ComboBox ItemsSource は ObservableCollection にバインドされています。

ObservableCollection は「Person」オブジェクトのコレクションです。プロパティ「Usage Count」を公開します。

「検索文字列」が空の場合、ObservableCollection の上位 30 レコードを表示する必要があります。"Person" クラスの "UsageCount" プロパティは、上位 30 レコードを決定します (つまり、最大の UsageCount を持つ上位 30 レコードを表示する必要があります)。UsageCount プロパティは動的に変化します。どうすればこれを達成できますか..助けてください。前もって感謝します :)

4

2 に答える 2

0

検索可能な並べ替えられたコレクションを処理するには、独自のオブジェクトを作成し、ObverservableCollection から継承し、Item の既定のプロパティをオーバーロードし、(通知する) SearchString プロパティを追加し、Person リスト全体の変更をリッスンし、変更 (SeachString の変更またはPerson の UsageCount) 人の新しいプライベート リストを作成し、NotifyCollectionChanged イベントを使用して通知します。

于 2012-05-01T09:10:04.300 に答える
0

ListCollectionView にバインドしない理由をフィルタリングする必要がある場合は、ここにアイデアがあります

in the View 

   ComboBox ItemsSource="{Binding PersonsView}" //instead of Persons

あなたのViewModelで:

public ListCollectionView PersonsView
{
    get { return _personsView; }
    private set
    {
        _personsView= value;
        _personsView.CommitNew();
        RaisePropertyChanged(()=>PersonsView);
    }
}

リストに入力したら

PersonsView= new ListCollectionView(_persons);

ビューのどこかに、フィルターを更新するコンボボックスの変更に応答する場所が明らかにあり、そこにフィルターを適用できます

_viewModel.PersonsView.Filter = ApplyFilter;

ApplyFilter は、表示される内容を決定するアクションです

//this will evaluate all items in the collection
private bool ApplyFilter(object item)
{
    var person = item as Person;
    if(person == null)
    {
        if(person is in that 30 top percent records)
            return false; //don't filter them out 
    }
    return true;
    }

    //or you can do some other logic to test that Condition that decides which Person is displayed, this is obviously a rough sample
}
于 2012-05-03T22:39:03.773 に答える