views:

46

answers:

1

I have a object that inherits from TabItem. I have a bunch of Database objects that will reuse the same code so I wanted only one TabItem class and then use DataTemplates to control how each object gets presented.

Problem is that the TabItem shows a collection of Objects and ObservableCollection is concrete.

I've pondered a few solutions but none of them work. Seems like I will have to create one class for each object type even when they will all be the same (except for ObservableCollection having different types). That's not very DRY.

I can't make a UserControl generic, I can't let the UserControl constructor take in a generic class ( unless I define the Type wihc I don't wan't to do). I guess creating a base UserControl class and then inheriting that will have to do. Does it inherit the XAML code as well or will I have to rely on styles and templates?

Am I missing something?

+2  A: 

Look into using DataTemplateSelector to provide flexibily in how you present your data in WPF.

Here are three sites that helped me:

Zamboni
Having not yet looked into Data templating I'm wondering if it's the solution. I have no problem applying the right DataTemplate. I have more of a problem in the Code Behind because I have to make a new class for each type to be able to put it into the correct ObservableCollection. I'll take a look.
Ingó Vals
Sorry I can't see how this is related to my problem. The real problem is the Generic class ObservableCollection. If I wan't to have one collection for multiple sibling types (all inherit from same base class) it doesn't work. Trying to typecast it into the collection results in some WPF error but no C# code error.
Ingó Vals