0

I've created a WPF custom ComboBox which has the ability to filter items according to a "search string". The ComboBox ItemsSource is bound to a ObservableCollection.

The ObservableCollection is a collection of "Person" object. It exposes a property "Usage Count".

Now if the "search string" is empty i have to show the Top 30 records from the ObservableCollection. The "UsageCount" property in the "Person" class decides the Top 30 Records(i.e. the the Top 30 records with the maximum UsageCount has to be displayed). The UsageCount property changes dynamically. How do i achieve this.. Please help. Thanks in advance :)

Dor Cohen
  • 16,769
  • 23
  • 93
  • 161
Girirsh
  • 48
  • 8

2 Answers2

0

To handle your searchable sorted collection, you can build your own object, inheriting from ObverservableCollection, overloading Item default property, adding a (notifying) SearchString property, listening to the changes of your Person entire list, building on change (change in SeachString Or in the UsageCount of a Person) a new private list of person, and using NotifyCollectionChanged event to notify it.

GameAlchemist
  • 18,995
  • 7
  • 36
  • 59
0

here's an idea, if you need filtering why not bind to a ListCollectionView

in the View 

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

in your ViewModel:

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

once you populate your List

PersonsView= new ListCollectionView(_persons);

somewhere in your view you obviously have a place responding to combobox's change, where you update the filter, you can put apply filter there

_viewModel.PersonsView.Filter = ApplyFilter;

where ApplyFilter is an action that decides what gets displayed

//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
}
denis morozov
  • 6,236
  • 3
  • 30
  • 45