views:

438

answers:

3

I have two listboxes one with all the projects and one with only active projects.

I have one observablecollection with all my projects in it bound to the listbox with all projects. What do I bind to the other listbox? Do I need to create a new collection and sort out the active ones. And when I add a new active project I need to add to both collections.

The behavour I'm looking for is to att a new Project to the collection with all projects so that both of the listboxes will be updated. Is it possible to do something like that?

+3  A: 

If you have two collections, you could subscribe to the CollectionChanged event on the master collection and use the notifications to synchronize the other list.

geofftnz
Please give me an example?
Emil C
+2  A: 

you should be able to carry that out with a CollectionView. CollectionView encapsulates sorting and filtering.

In you case, if you create a CollectionView that filters out inactive projects on your main collection and bind the second list to that view, you should be good to go.

EDIT: If only I read the full title and realised he was talking about Silverlight... Silverlight has no public implementation of ICollectionView, nor a CollectionViewSource, and only the Datagrid makes use of the interface anyway. Whoever up-voted should please cancel it, this is not a good answer to the question.

Back to the drawing board I guess. Right now I would say I would have two collections, and listen to the events on the first one (the full one) to - add/remove from the second one when an Active item is added/removed - add/remove a PropertyChanged handler on the added/removed items and - add/remove to/from the second collection when the Active property has changed. It is messy, but it might be packageable enough to reuse it when you need a collection filtered by a predicate.

Denis Troller
There is no CollectionViewSource to instanciate the CollectionView in Silverlight.
Emil C
Yes, I just realized that. I missed the part of the title (the big letters at the top...) that said "silverlight". Sorry about that.
Denis Troller
A: 

I would use two collections, the master list and the active projects list. Then subscribe to the collection changed event on the master list, handling all the actions

In addition you would need to add an event to the project object so that you could handle the occasion when the project goes from active to inactive and vice versa since the collection changed event only fires when the collection changes not when something it contains changes.

Graeme Bradbury