Reputation: 2664
I have the following property that I would like to use Linq to filter. This allows the binding in my view to update the UI just fine:
public ObservableCollection<Worker> Workers
{
get { return DataManager.Data.MasterWorkerList; }
}
These solutions were suggested in another post, but they don't allow the view to update:
get { return new ObservableCollection<Worker>(DataManager.Data.MasterWorkerList.Where(w => w.Known != true)); }
and
get
{
var workerList = DataManager.Data.MasterWorkerList.Where(w => w.Known != true);
return workerList.ToObservableCollection<Worker>();
}
DataManager
is a Singleton class and Data
is its instance. How can I use Linq filtering in this property without throwing off the UI updating?
Upvotes: 1
Views: 78
Reputation: 5650
While this isn't really "using LINQ filtering", if you're using ObservableCollection
you can use a PagedCollectionView
to add a filter to the collection.
PagedCollectionView items = new PagedCollectionView(Workers);
items.Filter = (o) => (o as Worker).Known != true;
Then bind to items
.
The problem with LINQ is that it creates a new collection, and so it breaks bindings. It's still possible to use it, but it can be a bit tiresome. Let me know if the above isn't what you need, and I'll try to figure something else out. ;)
EDIT: PagedCollectionView
is only available in Silverlight. If you're using WPF, then you can try something like this:
ICollectionView items = CollectionViewSource.GetDefaultView(Workers);
items.Filter = (o) => (o as Worker).Known != true;
Also, I recommend this article on how to display data in WPF (including sorting, filtering and such).
Upvotes: 1