Initially hide elements in ListView (uses ICollectionView and ObservableCollection)

848 views Asked by At

I am using a ListView which is bound to a ICollectionView (ListCollectionView -> ObservableCollection) While loading a file containing data the collection gets filled. Every data item has a boolean flag, which indicates if it must be shown in the listview or must be hidden.

Currently I have done this in an ugly way. I am filling first the collection (listview) with data. The user can see this. After filling it I start a routine which filters (ICollectionView.Filter) the items, which do not match. The listview item count shrink at that moment.

Is there a better way to implement this?

1

There are 1 answers

1
Thomas Levesque On

Just set the Filter before you start filling the collection:

ObservableCollection<Foo> collection = new ObservableCollection<Foo>();
ICollectionView view = CollectionViewSource.GetDefaultView();
view.Filter = YourFilterMethod;
// Fill the collection
collection.Add(...);