Take the 2-minute tour ×

I am new to WPF and I am creating an application which uses the TabControl. I am using a DataTemplateSelector and my datasource is an object I created from XML which have the properties "type" and "categoryID". I select my data template based on the "type" which works fine but I also need to create a tabitem for each categoryID during runtime. My problem is currently it creates a new TabItem for each object. How do I create a new tabitem based on the categoryID and place the dataTemplate on that tab and if the tab have already been created place the DataTemplate on that tab instead of creating a new one.

Thanks in advance!!

share|improve this question
You can use the ObservableCollection class and binding to the ItemsSource property. Before adding an item to the collection - check existance of this item in the collection, and if it exists - don't add. –  vorrtex Apr 4 '11 at 13:51
add comment

1 Answer

up vote 0 down vote accepted

I ended up using a CollectionViewSource with grouping and then I set the tabcontrol datacontext to the CollectionViewSource.

    private void PopulateTabControl()
        DataView = (CollectionViewSource)(this.Resources["DataView"]);
        tabcontrol.DataContext = DataView;            

    private void AddGrouping()
        PropertyGroupDescription grouping = new PropertyGroupDescription();
        grouping.PropertyName = "categoryID";
share|improve this answer
Is it possible to view the xaml? –  Emil Badh Apr 26 '12 at 11:46
add comment

Your Answer


