views:

3585

answers:

2

I've got an ObservableCollection of POCOs (Plain old CLR Objects) that I want to represent in a tabbed idiom. Prefereably using the MVVM pattern, is there a way to bind the collection of TabItems to the count of my POCO collection?

So, in this case if there are 3 items in my collection, I'd like to see 3 TabItems. Each TabItem would contain the same controls in the same location, each control bound to properties of the appropriate object in the collection.

I'm just looking for an overview of the approach I might use or a link to an example. If you need more info, feel free to ask.

Thanks.

+1  A: 

I'd probably create an ObservableColletion with your POCO items in it. You could then bind that ObservableCollection to any of the Silverlight Item Rendering controls. You'll have to modify the default rendering template to create your tabs...but using that method, your tabs will constantly be up to date with the items in the collection without having to put any code in the code behind file.

UPDATE

Here's a link to the Silverlight Forums where somebody built a TabControl using the ItemsControl with sample XAML code:

http://silverlight.net/forums/t/12271.aspx

...just scroll down a bit to see the sample.

Justin Niessner
Justin; thanks for the reply. The POCO items are in an ObservableCollection (I edited my post to reflect this). How would I then bind that collection in a way that would generate TabItems based on the number of items in the collection?
Steve Brouillard
@Steve - I would probably use a simple ItemsControl, create the custom template to render the ItemsControl as tabs, and then bind your ObservableCollection to the ItemsControl. The tabs will be properly generated based on the Items in the Collection.
Justin Niessner
Justin. Thanks. I think I follow the majority of what your saying. Can you point to an example? I think I could manage on my owen, but an example might push it along faster. Thanks.
Steve Brouillard
@Steve - Updated the answer.
Justin Niessner
@Justin - Thanks for the update. I did some searching on that forum with no luck, so thanks for the link.
Steve Brouillard
A: 

One way to do this is to use a value converter (IValueConverter) to return your POCO wrapped in a TabItem. I posted an example here as part of a related question. There is also sample xaml binding and injection of the ViewModel as a parameter to the value converter.

/jhd

John Dhom